Octave Programming Tutorial/Writing functions

Syntax
In Octave, function definitions use the following syntax:

Factorial Function
The factorial function, which takes exactly one argument and returns one integer, is as follows.

Save m-File
You can save the definition of the function in your working directory. Use the name of the function also for the filename - e.g. use the filename  for the definition of the function .

Maximum and Minimum of two integer Values
The following function, maxmin, returns the maximum and minimum value of two integers:

To call a function with multiple arguments, you specify multiple variables to hold the results. For example, one could write:

After executing, the variable 'big' would store the value '10' and the variable 'small' would store '7'. If fewer than two variables are used to hold the result then fewer outputs are returned. Writing

a = maxmin(13,5)

would store 13 in the variable 'a', and would discard the value of 5.

Return to the Octave Programming tutorial index