To create a function in MATLAB, you need to use the function
keyword followed by the output variable(s) and input variable(s). Here is the basic syntax of a MATLAB function:
main.m92 chars4 lines
function
keyword signals the start of the function block.[outputVariables]
is a list of variable names that the function returns to the calling routine.functionName
is the name of the function.(inputVariables)
is a list of arguments that the function accepts.function code
is the code that executes when the function is called.end
keyword marks the end of the function definition.Here's an example function definition that takes two numbers as input and returns their sum:
main.m98 chars5 lines
You can call the above function from the command window or from another script like this:
main.m127 chars5 lines
This will output 15
to the console.
gistlibby LogSnag