Programmer-defined Functions

Download Report

Transcript Programmer-defined Functions

Programmer-defined Functions
Vocabulary
1. More precise Global Idea
2. Vocabulary
1
General Concept, cont.
Huntsville, Alabama.
Applied
to the
Rocket
Project…
orbit.m
fuel.m
PARSEC (the Preliminary Analysis of Revolutionary Space Exploration Concepts) at NASA, in Huntsville, Alabama (2005)
2
Overall, it may seem to work like this
Programmerdefined
Function #1
Programmerdefined
Function #2
The client gives
requirements:
initial inputs.
Programmerdefined
Function #3
Programmerdefined
Function #4
Results the
client
wanted!
3
In reality, there is a boss (project manager)
Task 1
The client
initial data.
Results the
client
wanted!
Project
Manager
Task 2
Task 3
This may seem similar to EGR101 projects where, within a
team, students had to split a project into smaller tasks.
4
In reality, there may also be sub-tasks
Task 1
The client
initial data.
Results the
client
wanted!
Project
Manager
Task
1.1
Task
1.2
Task 2
Task 3
5
4. Vocabulary
How does this relate to
programming?
clc
clear
Function
definition #1
Return info
Function call, pass arguments
Main script
file
(Project
Manager)
Return info
Function
definition #2
Function
definition #n
6
Vocabulary, cont.
•
Main script file: The script file that contains the original overall project.
•
Function definition: the function header and the actual lines of code the function
has to execute. (a little file for each new keyword)
•
Function call: the command that calls upon the execution of the code that is inside
the function definition
– Usually placed within the main script file, but can also be within another
function definition. (A function CAN call another function you made!)
•
Passing arguments: giving inputs to the function definition.
•
Return info: final values that the function definition calculated and gives back
Main
script file
Function
definition
Function call, pass arguments
Return info
•
Collection variables: The variables which receive the return info
result =
7
Vocabulary: example1
• How many function calls does this program have?
clc
clear
%ask user for angle
angle = input('Enter an angle in degrees: ');
%calculate sine of the angle, display results
result = sind(angle);
fprintf('sine of %.2f degrees is %.2f\n', angle, result)
A.
B.
C.
D.
E.
1
2
3
4
5
8
Example, cont.
Main script file
clc
clear
(Project
Manager)
Function call
no return info
Function call
%collect
result =
IGNORE
return
info
clear
no return info
Function call, pass ‘Enter an angle….’
%collect
angle =
clc
Return value
Function call, pass angle
Return value
Function call, pass ‘string’, angle, result
Return-info
input()
sind()
fprintf()
9
Vocabulary: example2
• How many function calls does this program show?
clc
clear
%generate random value to evaluate grade
grade = rand*100;
A.1
B.2
C.3
D.More than 3
%find what letter that is, display result
letterGrade = changeToLetter(grade);
fprintf('With a grade of %.2f, that''s a(n) %c\n', grade,
letterGrade)
10
Example, cont.
Main script file
clc
clear
(Project
Manager)
Function call
no return info
Function call
%collect
letterGrade =
IGNORE
return info
clear
no return info
Function call, (pass nothing)
%collect
grade =
clc
Return value
Function call, pass grade
Return result
Function call, pass ‘string’, grade, letterGrade
Return-info
rand
changeToLetter()
fprintf()
11
Visual Example: Ordering Pizza
1. you place a call.
Pizza Place:
crust
ingredient1
ingredient2
2. pass arguments:
'thin crust'
'pepperoni'
'cheese'
5. collect the result!
…
3. execute code
(many variables used)
4. returns a
result
12
Key Points
• Be able to name each step in order, and describe them
• Be able to define each of the following:
–
–
–
–
–
main file
function definition
passing arguments
return values
collecting return-values
13