# How to make a function in Octave

In this lesson I'm going to show you how to create a function in Octave.

**What is a function?** It is a piece of code that can be executed when needed. The function code is separate from the main program. It can be in the same file or in a different file. Functions are very important in programming. They make the code of a program more concise and compact.

The general syntax for defining a function is as follows

**function** [y1, y2, ... ] = namefunction([x1,x2,...])

code of the function

**endfunction**

- The terms x
_{1},x_{2}... are the variables (input data) that the function receives as input from the call. - The terms y
_{1},y_{2}... are the variables (output data) that the function returns as output.

After defining a function, you can call and execute the function from different points in the program.

I'll give you a** practical example**

Type a script with any calculation operation. For example with 1;

1;

**Note**. If you start the script by typing function... the Octave interpreter thinks it is a function file rather than a script.

Then you need to create a named function **average()** that receives as input two numerical data x1 and x2 and returns as output the arithmetic mean y

1;

function y = average(x1,x2)

y = (x1+x2)/2;

endfunction

Now add in the program code a call to the average() function with two input values 2 and 4

1;

function y = average(x1,x2)

y = (x1+x2)/2;

endfunction

m=average(2,4);

disp(m);

The function receives the two input values x1=2 and x2=4, calculates the average y=(2+4)/2 and outputs the result y=3

Therefore, the program's output is 3

3

Now add a second call to the function with two different numbers.

For example, 4 and 6

1;

function y = average(x1,x2)

y = (x1+x2)/2;

endfunction

m=average(2,4);

disp(m);

m=average(4,6);

disp(m);

In this case the program calls the average() function twice from two different places.

- The first time it sends the values 2 and 4, receiving 3 as response.
- The second time it sends the values 4 and 6, receiving 5 as a response.

Therefore, the output of the program is

3

5

A function can also return multiple values in output.

For example, the function **[y1,y2] = pwr(x)** receives a numeric value (x) as input and produces two values as output.

1;

function [y1, y2] = pwr(x)

y1=x^2;

y2=x^3;

endfunction

[y1,y2]=pwr(2);

disp(y1);

disp(y2);

The function call sends x=2 as an input value.

The pwr() function calculates the square y1=4 and the cube y2=8 returning two output results.

4

8

If you want to anticipate the closure of the function and the return of the results, use the **return** statement.

Here is a practical example.

1;

function [y1, y2] = pwr(x)

if x<0

y1=0

y2=0

return

endif

y1=x^2;

y2=x^3;

endfunction

[y1,y2]=pwr(-2);

disp(y1);

disp(y2);

The return statement ends the function execution and returns the results y1=0 and y2=0 if the input value is negative.

In this case the output of the function is

0

0