130 likes | 299 Views
Programmer-defined Functions Vocabulary. More precise Global Idea Vocabulary. General Concept, cont. fuelsize.m. Applied to the Rocket Project…. Huntsville, Alabama. geometry.m. cost.m. engine.m. THE CLIENT. structure.m. orbit.m. fuel.m.
E N D
Programmer-defined FunctionsVocabulary More precise Global Idea Vocabulary
General Concept, cont. fuelsize.m Applied to the Rocket Project… Huntsville, Alabama. geometry.m cost.m engine.m THE CLIENT structure.m orbit.m fuel.m PARSEC (the Preliminary Analysis of Revolutionary Space Exploration Concepts) at NASA, in Huntsville, Alabama (2005)
Overall, it may seem to work like this Programmer-defined Function #1 Programmer-defined Function #2 The client gives requirements: initial inputs. Programmer-defined Function #3 Programmer-defined Function #4 Results the client wanted!
In reality, there is a boss (project manager) Task 1 The client initial data. Task 2 Project Manager Results the client wanted! Task 3 This may seem similar to EGR101 projects where, within a team, students had to split a project into smaller tasks.
In reality, there may also be sub-tasks Task 1.1 Task 1 Task 1.2 The client initial data. Task 2 Project Manager Results the client wanted! Task 3
4. Vocabulary How does this relate to programming? Function definition #1 Function call, passarguments Main script file clc clear Return info Function call, passarguments Function definition #2 Return info Function call, passarguments Function definition #n (Project Manager) Return info
Vocabulary, cont. Main script file • 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 variables that the function definition calculated and gives back • Collection variables: The variables which receive the return info Function definition Function call, passarguments Return info result =
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) • 1 • 2 • 3 • 4 • 5
Example, cont. clc Function call Main script file clc clear no return info clear (Project Manager) Function call no return info Function call, pass‘Enter an angle….’ input() %collect angle = Return value Function call, pass angle sind() %collect result = Return value Function call, pass‘string’, angle, result IGNORE return info fprintf() Return-info
Vocabulary: example2 • How many function calls does this program show? clc clear %generate random value to evaluate grade grade = rand*100; %find what letter that is, display result letterGrade = changeToLetter(grade); fprintf('With a grade of %.2f, that''s a(n) %c\n', grade, letterGrade) • 1 • 2 • 3 • More than 3
Example, cont. clc Function call Main script file clc clear no return info clear (Project Manager) Function call no return info Function call, (pass nothing) rand %collect grade = Return value Function call, passgrade changeToLetter() %collect letterGrade = Return result Function call, pass‘string’, grade, letterGrade IGNORE return info fprintf() Return-info
Visual Example: Ordering Pizza 1. you place a call. Pizza Place: crust ingredient1 ingredient2 … 2. pass arguments: 'thin crust' 'pepperoni' 'cheese' 3. execute code (many variables used) 5. collect the result! 4. returnsa result
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