MATLAB - Void Function



In MATLAB, every function can return a value, even if it's just an empty array or no output at all.However, you can create functions that perform actions or computations without explicitly returning a result. These functions are typically referred to as "procedures" rather than "functions," and they are often used for their side effects.

What is a "Void-Like" Function in MATLAB?

In MATLAB, a "void-like" function is a term used to describe a function that does not return any meaningful output or result but is used for its side effects, such as performing actions, modifying data, or displaying information. While every MATLAB function can technically return a value, these functions are designed to focus on their actions rather than their return values.

Creating a Void-Like Function in MATLAB

To create a "void-like" function in MATLAB, you follow the same basic structure as any other MATLAB function, but you do not explicitly return any values. Here's the basic syntax −

function voidFunction(arg1, arg2, ...)
   % Function body: Perform actions or computations
   % No explicit return statement
end

In above syntax −

  • voidFunction is the name of your function.
  • arg1, arg2, ... represents any input arguments your function may need.

Void-like functions in MATLAB are commonly used for −

  • Displaying information, messages, or results.
  • Modifying data structures in-place.
  • Performing actions that don't require a return value, such as saving files, plotting graphs, or updating the environment.

Examples of Void-Like Functions in MATLAB

Let us see a few examples void-Like functions in matlab −

Example 1: Function to Display Message

This function takes a message as input and displays it using fprintf, but it doesn't return any value.

function displayMessage(message)
   % Function to display a message
   fprintf('Message: %s\n', message);
   % No return statement
end

You can call the function as shown below −

displayMessage('Hello, World!');

On execution the output will be −

>> displayMessage('Hello, World!')
Message: Hello, World!;

Example 2: Void Function with Variable Modification

Let's create a void function that takes two numbers as input, calculates their sum, and displays the result without returning it −

function calculateAndDisplaySum(a, b)
   % Calculate the sum of 'a' and 'b'
   sumResult = a + b;
   
   % Display the result
   fprintf('The sum of %g and %g is %g\n', a, b, sumResult);
end

So in matlab the function will be saved as below −

sumresult

You can call the function like this in matlab command window −

>> calculateAndDisplaySum(5, 3);
The sum of 5 and 3 is 8
>> 

When the function is called , it will display "The sum of 5 and 3 is 8" in the MATLAB command window as shown above.

Example 3: Void Function with Actions

In this example, let's create a void function that generates a plot without returning anything −

function plotSineWave()
   % Generate a sine wave and plot it
   x = linspace(0, 2*pi, 100);
   y = sin(x);
   
   % Plot the sine wave
   plot(x, y);
   title('Sine Wave');
   xlabel('x');
   ylabel('sin(x)');
end

Let us create the function inside matlab first as shown below −

plot sine wave

When you execute above in matlab command window the output is as follows −

>> plotSineWave()
function sine wave
Advertisements