Octave:2> Functions

This entry is part [part not set] of 11 in the series GNU Octave

We have already looked into built-in functions in current series. Functions at the programming level is constituted by input variables, some precise operations done on input variable and output variables. All functionality that is required for an algorithm, might not be available as built-in function. Custom function not only fulfills this gap but at same time it makes the script cleaner and reusable. Functions are some chunk of code which takes input variable and return output after performing specific operation, and this same chunk can be used in multiple script. In this article we will be looking at different types of custom functions, their implementation and use. Like the earlier articles, we encourage readers planning to learn octave/Matlab to try the exercises marked as “Ex“.

Custom function can be of three types:

  • Defined using MATLAB files so “.m” files
  • Inline function
  • Anonymous function or lambda function

We will look into all three of them one by one in this article.

Sample Function
Function

Above image shows an implementation of basic function in Octave. A function is defined with function keyword, followed by return value and on the other side of equals sign should be name of function with input arguments. Algorithm defined inside function works on the input parameters and returns the value as return variable.

A function is a reliably way to implement the script. If some part of script is repeating the similar algorithm again and again with different input parameters, it is the correct way. Implement a function and call as many time as required. Function can itself be a part of script, but in that case function is reusable only in that script. If intention is to make use of function in other scripts too, it is better to save function as Matlab file.

A function can be ended with endfunction keyword in octave, but for Matlab compatibility we will use only end keyword.

Function will be usable only if name of “.m” file is same as name of function.

>> result = addFunction(3,4)
retval =  7
result =  7

We can see there are two output statements, as output is not suppressed using semi-colon inside function. Semi-colon is not used even when function is called. So it is a good practice to put semicolons to all expressions inside the function, unless it is required for debug.

Variables defined inside function are local variable. These variables are only valid inside the scope of function. As we can see the image of workspace variable retval is not a part of it.

Ex: Correct the function code so that retval is not visible.
Ex: Write a function that returns volume of sphere with provided with radius as input argument.

It is possible to return more than one value from the function as comma separated list.
function [retvalA, retvalB] = testFunction (input1, input2)
% algorithm to process input parameters and produce output
end

Inline Function

inline‘ is a built-in function from the file libinterp/octave-value/ov-fcn-inline.cc .
inline (STR, ARG1, …)
First string denotes the operation. If the second and subsequent arguments are character strings, they are used as the names of the arguments of the function.

>> multiply = inline('a*b', 'a', 'b')
multiply = f(a, b) = a*b
>> multiply(2,4)
ans =  8

Matlab help files say that ‘inline‘ function will be removed in future revisions. So, you better stick with the anonymous functions.

Anonymous Function

Anonymous function is not stored in a program file, but is associated with a variable whose data type is function_handle. These functions can accept multiple inputs and return only one output. Another limitation is that they can contain only a single executable statement.

>> div = @(a,b)a/b
div =
@(a, b) a / b
>> div(3,2)
ans =  1.5000

With the use of function start optimizing the script to make is reusable. Along with all other merits, function is also good for ease of debug and maintainability, as each part of script can be easily tested separately when implemented as independent functions. Let us know your query and questions in comment section.