Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the...

30
Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info, Documentation, and Code Body, saving it. 3. Testing in the command window 4. Testing with a main file 5. A new client comes along 1. Ignoring return values 1

Transcript of Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the...

Page 2: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

2

1. The client's wish:

Create a function which receives 3 arguments (a height, a mass and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category.

The client also gives these as indications:

BMI Category depends on BMI value:Underweight <=18.5Normal weight = 18.5-24.9Overweight = 25-29.9Obesity = BMI of 30 or greater

Page 4: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

2. Determine the return-info

Create a function which receives 3 arguments (a height, a mass, and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category.

4

Data calculated becomes return-info.When more than one variable comes back enclose the return list inside [ ] .

Page 5: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

2. Choose a function name

Create a function which receives 3 arguments (a height, a mass, and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category.

5

Name it so it represents what it does

Page 6: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

2. Create the parameter list

Create a function which receives 3 arguments (a height, a mass, and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category.

6

Inputs received become parameters.Separated by COMMAS all in ( ).

Page 7: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

2. Save the function

Save the function in the directory of your choice,

Hit F5 to run if you wish, but knowing that it'll crash right after changing your directory.

7

• But keep the filename MATLAB gives you by default.

DO NOT CHANGE IT, UNDER ANY CIRCUMSTANCES.

Page 8: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

2. Create the Documentation

Create a function which receives 3 arguments (a height a mass and the unit system used) and calculates the BMI value (body-mass-index: a measure of body fat) and the BMI category.

88Must use single % only without skipping one!.

Page 11: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

3. "Flow" of data

1111

2. Using only the given parameters, this code executes, thus solving for the return values.

One day, if needed, you may define new variables to compute intermediate steps.

Remember that all these variables are deleted when the function is done with its job.

Page 22: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

Almost Wrapping Up

Meet with the client, give the software, go on vacations…

But another client just walks in, having heard of the bmiCalculator… His request is slightly different: "I just care about the category, not the actual value of the bmi.

Can you help me?"

22

Page 25: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

25

5. Ignoring Return Values

Suppose a stack of books What if you want the 3rd

book (the yellow one) from the top?

You must collect the 1st and 2nd (red and green) before collecting the 3rd (yellow).

It is exactly the same with the return-values.• They come out in order.• To access the 3rd return value the code must collect the 1st and

2nd.

Page 26: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

5. Ignoring Return Values

>>>>>>> Every variable UP TO the one wanted must be collected in a variable. (not the ones after)

Trick: name the variable trash, dummy, ignore, a word that obviously reflects that it is not-to-be used later.

You may even delete that variable specifically using clear26

Try it! Look at the Workspace.

Page 28: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

28

Previous version of MATLAB Previous versions of MATLAB do not allow the use

of the ~ symbol.

Use a dummy variable name to store the data. The name reminds YOU (programmer) that this variable is of no use later on, so choose wisely!

It is a MUST to collect all the return data to the left of the one wanted.

Page 30: Functions General Example (+1return values, +1 parameters) 1. The client's wish 2. Creating the function 1. Function's name, Parameter list, Return-info,

Wrapping Up

Go type this example step-by-step and make it work. At most, this should take 15 minutes.

In order:

1. Create a function file.

2. Save it and change to the directory where it is.

3. Test and experiment in the command window

4. Create the main file afterwards

Only practice makes perfect, and the more functions you create, the easier codes will get:

INCLUDING the final project!30