Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 -Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. How it works Programmer.

Similar presentations


Presentation on theme: "1 -Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. How it works Programmer."— Presentation transcript:

1 1 -Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. How it works Programmer

2 2 -Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. How it works User

3 3 1.a. Goals of overall chapter 1. Understand all the advantages of writing programmer- defined functions 2. Know all the vocabulary (7) involved Function definition, Function call, Parameters vs. Arguments, Return info, Documentation, Code body (function body) 3. Easily create a programmer-defined function file and position all the elements correctly 4. Easily test and use a programmer-defined function

4 1.b. Goals of this lesson Understand the overall idea of what a function is Get a mental picture of how they work Know the many advantages of writing/using functions 4

5 5 2. General Concept sin(), cos(), fprintf(), mod(), input() were called built-in or library functions. These functions already exist in the MATLAB directory. They “come with” the software. This chapter introduces programmer-defined functions. As the vocabulary states the function is defined (written) by the programmer (you!). It does not pre-exist in the MATLAB directory.

6 6 General Concept cont. Used in every decent piece of software that exists. EGR101 – Rocket Project

7 7 General Concept cont. Huntsville Alabama. PARSEC (the Preliminary Analysis of Revolutionary Space Exploration Concepts) at NASA in Huntsville Alabama. Orbit Specialist Fuel Specialist Size Fuel Tank Structure Specialist Select/Design Engine Vehicle Geometry Estimate Cost THE CLIENT Can you see advantages to working like this? Applied to the Rocket Project…

8 3. Advantages 1. Focus: The developers are concerned with the goals of the function, not being distracted by other details of the project. 2. Portability: Instead of one script file that contains the entire program, the software is divided into smaller files. 1. Various engineers can work on their parts simultaneously. 2. Engineers can develop code peacefully in their private cube or even take work home or on business travel. 3. Engineers can send pieces of codes to other colleagues who are just as specialized as them providing feedback and improvements! 4. One engineer can participate in multiple projects: the fuel requirements may be the same for two different rockets… 8

9 9 3. Memory efficiency: One code: lots of variables  Smaller code and functions: lots of calculations but only the results are stored. The intermediate calculations are cleared from memory 4. Easier to debug: One code: what does not work???????? Smaller code and functions: Each function can be tested separately regardless of work by other colleagues. Assumptions have to be made but the function itself can be tested. Advantages, cont.

10 4. How it works Just like big projects 10 The client gives initial data. Results the client wanted! Task 1 Task 2 Task 3 Project Manager This may seem similar to EGR101 projects where within a team, students had to split a project into smaller tasks.

11 11 5. In Programming… Main script file clc clear Function definition #1 Function definition #2 Function definition #n Passing data to the function Passing data to… Getting results back…

12 12 5. Vocab. "Main Script File" Main script file (Project Manager) clc clear Function definition #1 Function definition #2 Function definition #n Passing data to the function Passing data to… Getting results back… "Main Script File" i.e. THE BOSS.

13 13 5. Vocab. "Function Definitions" Main script file (Project Manager) clc clear Function definition #1 Function definition #2 Function definition #n Passing data to the function Passing data to… Getting results back… "Functions Definitions" i.e. Each smaller piece of code.

14 14 5. Vocab. "Calling" Main script file (Project Manager) clc clear Function definition #1 Function definition #2 Function definition #n Getting results back… "Calling the function" i.e. "Execute this!"

15 15 5. Vocab. "Passing Arguments" Main script file (Project Manager) clc clear Function definition #1 Function definition #2 Function definition #n Passing data to the function Passing data to… Getting results back… "Passing Arguments" i.e. Giving Inputs

16 16 5. Vocab. "Return Values" Main script file (Project Manager) clc clear Function definition #1 Function definition #2 Function definition #n Passing data to the function Passing data to… Getting results back… "Return Values" i.e. Outputs

17 17 Vocabulary: Summary 1. Main script file: The script file that contains the original overall project. 2. Function definition: the actual lines of code the function has to execute. 3. Function call: the command that calls upon the execution of the code that is inside the function-definition 1. Usually placed within the main script file but can also be within another function-definition. Yes, a function can call a 1 st function that can call a 2 nd function that calls a 3 rd function,… 4. Passing arguments: giving inputs to the function definition. 5. Return info: final values that the function-definition calculated and gives back

18 Wrapping Up Any decent software has many many many functions. Advantages: 1. Focus/modularity 2. Portability 3. Memory efficient 4. Easy to debug Disadvantage: a lot more files to keep track of! Works like any job in real life. A big-boss delegates smaller tasks to other people. These other people may choose to delegate even more! Vocabulary: function call, function definition, passing arguments, return values 18


Download ppt "1 -Defined Functions 1. Goals of this Chapter 2. General Concept 3. Advantages 4. How it works Programmer."

Similar presentations


Ads by Google