To define a function in Matlab, you use the function
keyword followed by the function name and input arguments in parentheses. Then comes the body of the function, followed by the output arguments in square brackets (if there are any).
Here's an example of a function that returns the sum of two numbers:
main.m62 chars4 lines
In this example, the function is named add_numbers
and takes two input arguments, a
and b
. The body of the function simply adds these two values together and assigns the result to the variable result
, which is then returned as the output of the function.
To call this function, you would simply pass in two numbers as arguments like this:
main.m34 chars4 lines
This would return the sum of 2 and 3, which is 5.
gistlibby LogSnag