1 -defined functions 1. goals of this chapter 2. general concept 3. advantages 4. how it works...

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

Upload: elwin-tucker

Post on 11-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

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

3

1.a. Goals of overall chapter1. Understand all the advantages of writing programmer-

defined functions

2. Know all the vocabulary (7) involvedFunction 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

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

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.

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

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…

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

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

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

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.

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

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.

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

11

5. In Programming…

Main script file

clcclear

Function definition #1

Function definition #2

Function definition #n

Passing data to the function

Passing data to…

Passing data to…

Getting results back…

Getting results back…

Getting results back…

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

12

5. Vocab. "Main Script File"

Main script file

(Project Manager)

clcclear

Function definition

#1

Function definition

#2

Function definition

#n

Passing data to the function

Passing data to…

Passing data to…

Getting results back…

Getting results back…

Getting results back…

"Main Script File"i.e.

THE BOSS.

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

13

5. Vocab. "Function Definitions"

Main script file

(Project Manager)

clcclear

Function definition

#1

Function definition

#2

Function definition

#n

Passing data to the function

Passing data to…

Passing data to…

Getting results back…

Getting results back…

Getting results back…

"Functions Definitions"i.e.

Each smaller piece of code.

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

14

5. Vocab. "Calling"

Main script file

(Project Manager)

clcclear

Function definition

#1

Function definition

#2

Function definition

#n

Getting results back…

Getting results back…

Getting results back…

"Calling the function"i.e.

"Execute this!"

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

15

5. Vocab. "Passing Arguments"

Main script file

(Project Manager)

clcclear

Function definition

#1

Function definition

#2

Function definition

#n

Passing data to the function

Passing data to…

Passing data to…

Getting results back…

Getting results back…

Getting results back…

"Passing Arguments"i.e.

Giving Inputs

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

16

5. Vocab. "Return Values"

Main script file

(Project Manager)

clcclear

Function definition

#1

Function definition

#2

Function definition

#n

Passing data to the function

Passing data to…

Passing data to…

Getting results back…

Getting results back…

Getting results back…

"Return Values"i.e.

Outputs

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

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 1st function that can call a 2nd function that calls a 3rd function,…

4. Passing arguments: giving inputs to the function definition.

5. Return info: final values that the function-definition calculated and gives back

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

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