﻿Programming Course - Functions

## Functions

### Arguments, return value and the my operator

As with built-in functions, a user-defined function may receive a list of arguments, perform a set of commands and return a value.

The my operator specifies private variables of the function.

#### Example #1

The following program contains a user-defined function, named calc_average, that receives two numbers and returns their average.

The program passes the numbers 6 and 10 to the subroutine and then prints the returned average.

```#!/usr/bin/perl

\$a = 6;
\$b = 10;

\$average = calc_average (\$a, \$b);  # subroutine invocation.

print "\$average\n";

# subroutine definition starts here

sub calc_average {

my (\$x, \$y) = @_;
my (\$result);

\$result = (\$x + \$y) / 2;
return \$result;
}

# the program will print: 8
```

#### Notes

• The subroutine is invoked by the command:
```\$average = calc_average (\$a, \$b);
```
Where \$a and \$b are the arguments passed to the subroutine.

• Upon subroutine invocation, a special array variable is formed by Perl which contains the list of arguments passed to it. This array is called @_, and it lasts throughout the duration of the subroutine.

In this example, inside calc_average the @_ array will contain (6, 10).

• Although it is possible to relate to the elements of @_ inside the subroutine as \$_[1], \$_[2] etc., it is customary to directly assign the elements to variables. Thus, subroutine definitions often start with a construct like:
```my (\$var1, \$var2, \$var3 ...) = @_;
```
And in this example:
```my (\$x, \$y) = @_;
```

• We use the my operator to assure that variables inside the subroutine will not interfere with variables having the same name outside of the subroutine. These variables will also not be recognized outside of the subroutine. In other words, the my operator declares a list of variables (written in parentheses, if there are more than one) that exist only inside the subroutine.

• The return function is used inside subroutines to specify the value that will be returned by the subroutine to the outside program (or to the place in the program from which the subroutine was invoked).

In this example, the subroutine returns the value of \$result, using the command:

```return \$result;
```

A function may also return a list value, e.g.

```return (\$n, \$m, \$k);    or
return (@arr);
```

• The subroutine invocation expression, e.g.
```calc_average (\$a, \$b);
```
then gets the value returned by the subroutine. In this example, it will become 8.

Normally, the subroutine invocation expression is assigned to some variable (here: \$average), which will then contain the subroutine return value:

```\$average = calc_average (\$a, \$b);
```

However, note that you can also use the subroutine invocation expression directly in some other operation. e.g. you can directly print it:

`print calc_average(\$a, \$b), "\n";`
or include it in an arithmetical calculation:
```\$net = calc_average(\$a, \$b) - \$background;
```