Main Content

Functions

Code that accepts inputs and returns outputs

MATLAB® includes a wide range of predefined functions for computational tasks. For basics on how to call these functions, see Calling Functions.

As you write code, you can define your own functions to reuse a sequence of commands. For instance, create a function in a program file to calculate the area of a circle.

function A = areaCircle(R)
     rSquared = R.^2;
     A = pi.*rSquared;
end
Then, call the function as you would call a predefined function.
r = 10;
a = areaCircle(r);

For more information, see Create Functions in Files.

Categories