1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition...
-
Upload
sherilyn-hutchinson -
Category
Documents
-
view
217 -
download
3
Transcript of 1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition...
1
Functions1 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
1. Suppose this project…
2
Suppose this if statement will need to be repeated a lot. We plan to ask for all:1) Quiz…2) Lab…3) Exam…
DO NOT: copy/paste each timeDO: create 1 function, and CALL it
each time.
Overall Flow
3
Main script
file
changeToLetter()CALL it each
time it is needed!
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
Step2. I/O of the function
One parameter, 1 return-value
5
changeTo Letter()
Numerical value
The letter that goes with it
3. Create the definition
6
The keyword function FIRST The RETURN-INFO (i.e. OUTPUT) second The NAME OF THE FUNCTION third The PARAMETER (i.e. INPUT) last
Save the function file as soon as possible
7
The name of the file MUST match the name of the function.
MATLAB knows that, so it offers the correct name.
Change the directory to where you want, then hit save.
3. Create the definition
3. Create the definition
8
The DOCUMENTATION is important
A function's doc ALWAYS shows how to use it.
The DOCUMENTATION is important
A function's doc ALWAYS shows what it does.
3. Create the definition
9
3. Create the definition The code block actually solves the problem
10
This is the mini-code that needs to be executed multiple times with various values of numericalGrade
This is all new.
11
MATLAB knows this is an input, since it is placed in the <parameter list>.
4. "Flow" of data
12
MATLAB knows this an output, since it is placed in the <return value>.
4. "Flow" of data
5. Testing: "FUNCTION CALL"
Now that the function seems complete, be sure to test that even this small function works!
FACT: most functions require inputs, hence F5 will not work and results in the following error:
13
14
After hitting F5 to run this, MATLAB has no value to use!
5. Testing: "FUNCTION CALL"
I/O of the function
One parameter, 1 return-value
15
Again, there is no way to test this function using F5. Somehow, the function needs a numerical value!
changeTo Letter()
Numerical value
The letter that goes with it
5. Testing: "FUNCTION CALL"
The only advantage to hitting F5 to run is that it prompts to change directory to that particular function file before it crashes. THAT, we want.
Now, we are in the correct setup to test.
16
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.
"Flow" of data
18
>> changeToLetter(88.99)
MATLAB knows the keyword, since it sees it in the current directory.
1
"Flow" of data
19
>> changeToLetter(88.99)
MATLAB passes the argument 88.99, which gives a value to the parameter numericalGrade
2
"Flow" of data
20
>> changeToLetter(88.99)
MATLAB executes the code, thus solving for equivalentLetter3
"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.
"Flow" of data
22
>> changeToLetter(88.99)
ans = B
MATLAB collects the value in its default VARIABLE ans, unless otherwise specified.
5
5. Testing
For example:
23
Note: the letter grade "A" seems to work.
Note: the letter grade "C" seems to work.
24
5. Testing
No matter what if statement gets executed, MATLAB has a return value for equivalentLetter, even in case of an error.
25
IMPORTANT:No matter what path MATLAB takes (i.e. which if statement happens), the variable equivalentLetter is always assigned a value.
All outputs must have a value
All outputs must have a value
The following would NOT be a good function:
26
???Suppose numericalGrade is set to -4 by the call.
disp()
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.
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
Project 1
Fill in with the variables of the main file
29
Replace the parameter and return-info by the final variables to use.
Project 1 - Testing
Remember to test again, fully.
30
Project 1 - Testing
Even small mistakes can be caught!
EASY TO DEBUG: In the command window, we had no error! This means:
The function is correct, the fprintf() is not.
31
'ERROR'
Project 1 - Testing
Even small mistakes can be caught!
32
'ERROR'
The ASCII code.
Project 1 - Testing
Even small mistakes can be caught!
33
Project 1: done.
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
Testing (as you go!)
Before displaying, see if it works:
36
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
Project 2, display
The for loop changes the index k automatically, and displays one grade each time.
The format modifiers %10s, %10.2f, etc.. allow the columns to be aligned properly.
38
10 10
10 7
Project 2: done.
Recall the Function Calls
Project 1:
letter = changeToLetter(grade);
Project 2:
letters(k) = changeToLetter(numericalGrade(k));
39
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