Defining functions

In its simplest form, the definition of a function named ``boo'' looks like this:

function zz = boo(i,j)
  zz = i + j;

Here ``i'' and ``j'' in ``zz = boo(i,j)'' are the function's arguments: When the function boo is called, the arguments i and j are used to hold the values given in the call. In most cases, you will want to get some information back from the functions you define. The variable ``zz'' in ``zz = boo(i,j)'' is the one that will hold the value to be returned by the function.

function zz = increment(x)
  if(nargin >= 1)
    zz = x + 1;
  else
    zz = 0;
  end

When the function is called, the variable ``nargin'' is automatically initialized to the number of arguments passed to the function.



Department of Mathematics
Last modified: 2005-10-21