Calling a function, also known as invoking a function, is a programming technique that transfers program control to the needed function, completing the specified tasks and transferring program control back to the main program. The necessary arguments or parameters are simply passed into the function name to invoke it.
We will learn about calling both built-in and user-defined functions in MATLAB in this article.
1: How to Call MATLAB Built-in Functions?
MATLAB includes many built-in functions such as sqrt(), mean(), min(), max(), and many more that are used for performing computational tasks. We use two methods to call these functions.
1.1: Calling Built-in Functions Using MATLAB Command Window
We can call the MATLAB built-in function by simply passing the required arguments using the command window.
For example:
sqrt(num)
1.2: Calling Built-in Functions Using MATLAB Script File
We can call the MATLAB built-in function by creating a script file and call the function by simply passing the required arguments. For example:
sqrt(num)
2: How to Call MATLAB User-Defined Functions?
The user-defined functions in MATLAB operate in a similar way as the built-in functions. These functions are called in multiple ways, including:
2.1: Calling User-Defined Functions Using MATLAB Command Window
Once we have created and saved a function, we can call this MATLAB user-defined function using the command window by simply passing the required arguments. For example:
result = x + y;
end
We will now utilize the command window to call the user-defined function add().
2.2: Calling User-Defined Functions Using MATLAB Script File
Once we have created and saved a function, we can call this MATLAB user-defined function using the script file by simply passing the required arguments. For example:
result = x + y;
end
Now we will call this user-defined function to add() in another script file.
Conclusion
MATLAB supports two types of functions, built-in and user-defined functions; these functions are called in MATLAB using two ways, either through a command window or a script file. This tutorial provided you with enough guidance on how to call the function through the command window and script file, with some simple examples implemented in MATLAB for better understanding.