1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition...

40
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2

Transcript of 1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition...

Page 4: 1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.

2. Recall The Layout

Open the editor and get ready to type:

4

function <return info> = <function name>(<parameter list>)% <documentation>% <more documentation…>

% author/rights/edition…

<function body>

1.2.

..

The inputs to the function

The outputs to the function

Page 17: 1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.

5. Testing: "FUNCTION CALL"

Experiment in the command window

WRITE FUNCTION CALLS Replace the parameter by a real value The function returns a value. It is stored in the default

variable name ans as usual.

17

Note: the letter grade "B" seems to work.

We call this a "function call". We "Call Upon The Execution" of the code.

Page 21: 1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.

"Flow" of data

21

>> changeToLetter(88.99)

MATLAB returns the VALUE of equivalentLetter.

4

It is called "return-value", not "return-variable". You will never see the variable equivalentLetter in the workspace. It gets deleted as soon as the function has completed its job.

Variables in a function are called "local variable", since they are never ever seen by the main code.

Page 27: 1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.

All outputs must have a value In this case:

Each return-info must have a value when the function is done. 27

If the call decides to store the expected letter grade in a variable, none exist!

If the call does not ask for a return-value, all is ok.

Page 28: 1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.

Step4. again.

Now that the function has been proven to work properly, call your boss/colleagues to put the main project together.

Project1: ask for one grade, convert it, and display

Project2: ask for lots of grades, convert them, and display

28

Page 34: 1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.

Project 2

Combines function calls

with loops and arrays

Assume the following algorithm:%prompt how many grades%for each grade

%prompt for each grade, store in array%convert to a letter and store in an array

%display table of grades with the letter

34

Page 37: 1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.

Project 2, display

The two arrays cannot be combined into 1 single matrix, since they are of two different data types.

Cells-arrays (who can combine different data types), do not display nicely. Here is proof from the command window (where you can continue to experiment):

Use a for loop to traverse the arrays and display each element in a formatted manner. 37

Page 40: 1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.

Wrapping Up

To create a function:1. Open the editor, type function, the return-info, the name, the

parameters, the documentation, then the code

2. Save the file. Match the filename with the name of the function.

3. Change the directory so the function shows in the Current Directory panel

4. Test thoroughly in the command window

5. Once complete, build the main code

6. Test thoroughly again!

Be courageous to combine arrays, functions, loops together from now on!

40