Copyright © Michael Richmond. This work is licensed under a Creative Commons License.

# Using input and output arguments in functions

Suppose that we want to write our own version of the sqrt function, which will take the square root of some number. Here's a first attempt:

 ``` function my_sqrt_a value = 36; result = sqrt(value); fprintf(1, 'result is %8.2f \n', result); end ```

It seems to do the right thing when I run it.

```
--> my_sqrt_a
result is     6.00

```

But there's (at least) one big drawback of this function compared to the built-in sqrt function.

```

Q: What is the biggest drawback of my version?

```

Right -- if I want to take the square root of a different number, not 36, then I need to

• open an editor to modify the source code
• change the value assigned to the variable value
• save the new source code
• run the new function

It would be much nicer if we could modify the function so that the user could simply provide the number to be rooted as an argument. Fortunately for us, it is easy to add input arguments to a MATLAB function:

 ``` function my_sqrt_b (input_arg) value = input_arg; result = sqrt(value); fprintf(1, 'result is %8.2f \n', result); end ```

Now, when the user invokes the function by typing its name, he can supply the number to be rooted inside parentheses, like this:

```

--> my_sqrt_b(36)
result is     6.00
--> my_sqrt_b(42)
result is     6.48

```

Notice that an error will occur if the user does NOT provide an argument:

```

--> my_sqrt_b
In /home/richmond/classes/phys559/args/my_sqrt_b.m(my_sqrt_b) at line 3
In docli(builtin) at line 2
In base(base)
In base()
In global()
Error: Undefined function or variable input_arg

```

and an error will also occur if the user does not provide the proper number of arguments:

```

--> my_sqrt_b(23, 54, 3)
Error: Too many inputs to function my_sqrt_b

```

#### Output arguments

Now that we know about input arguments, I can create my own version of a function which computes the square root of a value supplied by the user. The version shown below doesn't print the words "result is", but simply prints the value of the square root.

 ``` function my_sqrt_c (input_arg) value = input_arg; result = sqrt(value); fprintf(1, '%f \n', result); end ```

Will this act exactly like the real sqrt function? Let's find out: I'll compute the square root of 23 and assign it to the variable a.

First, I'll use the real MATLAB function.

```
--> a = sqrt(23);
--> a
ans =
4.7958
--> a + 3
ans =
7.7958

```

Now, I'll try to the same operations with my version.

```

--> a = my_sqrt_c(23)
Error: Too many outputs to function my_sqrt_c
--> a
Error: Undefined function or variable a

```

Whoops! That didn't work. The reason is that my function was simply PRINTING the value of the square root; it was not RETURNING that value as an output from the function.

In order to RETURN a value from a function, one must modify the line declaring the function like so:

 ``` function [result] = my_sqrt_d (input_arg) value = input_arg; result = sqrt(value); end ```

Notice that output arguments appear in [square brackets], while input arguments appear in (parentheses).

With this change, my version finally does act just like the standard one:

```
--> my_sqrt_d(23)
ans =
4.7958
--> a = my_sqrt_d(23)
a =
4.7958
--> a + 3
ans =
7.7958

```

#### Exercises

1. Write a program which takes two input arguments, multiplies them together, and returns the product. Call the program my_mult.m. It should work like this:
```
--> a = my_mult(3, 4);
--> a
ans =
12
```
2. Write a program which takes three numbers as input, and returns a list of two outputs: the smallest value, then the largest value. Call the program min_and_max.m. For example,
```
--> [a b] = min_and_max(10, 5, 3);
--> a
ans =
3
--> b
ans =
10
```

#### For more information

You can find the programs used as examples in this document in the examples directory , and listed below.

Copyright © Michael Richmond. This work is licensed under a Creative Commons License.