Engineering Problem Solving with C EtterIngber Chapter 5

Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Modular Programming with Functions Engineering Problem Solving with C++, Second edition, J. Ingber 1

Modular Programming with Functions 4 Modularity 4 Programmer Defined Functions 4 Parameter Passing Engineering Problem Solving with C++, Second edition, J. Ingber 2

MODULARITY Engineering Problem Solving with C++, Second edition, J. Ingber 3

Modularity 4 A problem solution contains numerous functions. – main() is a programmer defined function. – main() often references functions defined in one of the standard libraries. – main() can also call other programmer defined functions. 4 Functions, or modules, are independent statement blocks that are written to perform a specialized task. Engineering Problem Solving with C++, Second edition, J. Ingber 4

Modules 4 A C++ source program can be thought of as an ordered collection of executable tasks: • input data • analyze data • output results 4 In C++ these tasks can be performed using programmer defined functions and types. Engineering Problem Solving with C++, Second edition, J. Ingber 5

Functions 4 Complex problems can be broken down into sub tasks, each of which is easy to implement in C++. 4 Advantages to using programmer defined functions and types, as opposed to writing the entire solution in main(): –Multiple programmers –Testing/Debugging/Maintaining –Reduce duplication of code Engineering Problem Solving with C++, Second edition, J. Ingber 6

Functions 4 Pre-defined – standard libraries 4 User defined Engineering Problem Solving with C++, Second edition, J. Ingber 7

Pre-defined Functions - Example #include <iostream> #include <cmath> using namespace std; int main() { double angle; cout << “input angle in radians: “; cin >> angle; cout << “nthe sine of the angle is “ << sin(angle) << endl; return 0; }//end main Engineering Problem Solving with C++, Second edition, J. Ingber 8

function definiton function prototype PROGRAMMER DEFINED FUNCTIONS Engineering Problem Solving with C++, Second edition, J. Ingber 9

Functions: Terminology 4 Function Prototype – describes how a function is called 4 Function Call – references a function 4 Function Arguments – used in the function call 4 Function Definition – function header – statement block 4 Formal Parameters – used in function definition 4 Formal parameters must agree with arguments in order, number and data type, but the identifies can be different. Engineering Problem Solving with C++, Second edition, J. Ingber 10

Programmer Defined Functions 4 Can be defined to: – return a single value to the calling function. – perform a data independent task. – modify exiting data via the parameter list (pass by reference. ) Engineering Problem Solving with C++, Second edition, J. Ingber 11
![Function Definition 4 Syntax: return type function name([parameter list]) { //statements return type; } Function Definition 4 Syntax: return type function name([parameter list]) { //statements return type; }](http://slidetodoc.com/presentation_image_h2/6cf48a0ffa36f9a651fcc951b5a89740/image-12.jpg)
Function Definition 4 Syntax: return type function name([parameter list]) { //statements return type; } Engineering Problem Solving with C++, Second edition, J. Ingber 12

Value Returning Functions 4 A value returning function returns a single value to the calling program. 4 The function header declares the type of value to be returned. 4 A return statement is required in the statement block. Engineering Problem Solving with C++, Second edition, J. Ingber 13

n! example • n! = n*(n-1)*(n-2)*…*1 • n is a positive integer • 0! is 1 by definition Engineering Problem Solving with C++, Second edition, J. Ingber 14

Example - factorial function //function definition: n! = n*(n-1)*(n-2)*…*1, // 0! is 1 by definition //Function fact returns n! //Function fact assumes n is non-negative integer int fact(int n) //function header { int nfact = 1; while(n>1) { nfact = nfact*n; n--; }//end while block return nfact; }//end fact Engineering Problem Solving with C++, Second edition, J. Ingber 15

Function Call int fact(int n); //function prototype #include <iostream> using namespace std; int main() { int n; cin >> n; if(n>=0) cout<<n<<"! is " <<fact(n)<<endl; //argument: n else cout <<"not defined for negative numbers" <<endl; return 0; }//end main Engineering Problem Solving with C++, Second edition, J. Ingber 16

Calling a function- second example int fact(int); //function prototype #include <iostream> using namespace std; int main() { int n, factorial; cin >> n; if(n>=0) { factorial = fact(n); //function call cout << n <<“! is ” << factorial << endl; } else cout << “Not defined for negative numbers” << endl; return 0; }//end main Engineering Problem Solving with C++, Second edition, J. Ingber 17

void Functions 4 A void function declares void as a return type. 4 A return statement is optional. 4 If a return statement is used, it has the following form – return; Engineering Problem Solving with C++, Second edition, J. Ingber 18

Example of void function //Scale data //function header void scale. Data(vector<double>&data, double sf) { for(int i=0; i<data. size(); ++i) { data[i] *= sf; //data[i]=data[i]*sf; }//end for return; //return is optional } //end scale. Data Engineering Problem Solving with C++, Second edition, J. Ingber 19

pass by value pass by reference storage class and scope PARAMETER PASSING Engineering Problem Solving with C++, Second edition, J. Ingber 20

Parameter Passing 4 C++ supports two forms of parameter passing: – Pass by value. – Pass by reference. Engineering Problem Solving with C++, Second edition, J. Ingber 21

Parameter Passing - pass by value – Pass by value is the default in C++ (except when passing arrays as arguments to functions). – The formal parameter receives the value of the argument. – Changes to the formal parameter do not affect the argument. Engineering Problem Solving with C++, Second edition, J. Ingber 22

Example: … int fact(int); int main() { int n, factorial; //1 cin >> n; //2 if(n>=0) { factorial = fact(n); //3 cout << n <<"! is " << factorial << endl; //7 }//end if return 0; }//end main //Function Definition int fact(int num)//4 { int nfact = 1; //5 while(num>1) { nfact = nfact*num; num--; } return(nfact); //6 } //end fact Engineering Problem Solving with C++, Second edition, J. Ingber 23

Program Trace: pass by value //Function Definition int fact(int num)//4 … int fact(int); { int main() int nfact = 1; //5 { while(num>1) int n, factorial; //1 { cin >> n; //2 nfact = nfact*num; if(n>=0) num--; { } factorial = fact(n); //3 return(nfact); //6 cout << n <<“! is “ } //end fact << factorial << endl; //7 }//end if return 0; }//end main call fact() Memory Snapshot: main() int factorial ? 2 -> 3 ? 3 -> 7 -> 3 6 5 -> int nfact 1 1 6 6 -> return nfact 1 -> int n ? 4 -> int num 3 Note: value of n in main has not changed. 24

Parameter Passing - pass by reference 4 Pass by reference allows modification of a function argument. 4 Must append an & to the parameter data type in both the function prototype and function header void get. Date(int& day, int& mo, int& year) 4 Formal parameter receives the address of the argument. 4 Any changes to the formal parameter directly change the value of the argument. Engineering Problem Solving with C++, Second edition, J. Ingber 25

Example - pass by reference #include <iostream> using namespace std; void swap(double&, double&); //function prototype int main() { double x=5, y=10; swap(x, y); //function call; x y are arguments cout >> “x = “ << x << ‘, ’ << “ y= “ << y << endl; return 0; } //end main Output is: x = 10, y = 5 Engineering Problem Solving with C++, Second edition, J. Ingber 26

Example - pass by reference //Function swap interchanges the values of two variables //function definition void swap(double& x, double& y) //function header { double temp; //local variable temp = x; x=y; y=temp; return; //optional return statement }//end swap Engineering Problem Solving with C++, Second edition, J. Ingber 27

Program Trace: pass by reference //Function Definition void swap(double& x, double& y) #include <iostream> using namespace std; { double temp; // void swap(double&, double&); //prototype temp = x; int main() { x=y; double x=5, y=10; y=temp; swap(x, y); //x y are arguments return; //optional cout << "x = " << x << ', ' }//end swap << " y= " << y << endl; return 0; } //end main Memory Snapshot: main() swap() double x double y double temp ? 5 double x 5 double y 10 call swap() Engineering Problem Solving with C++, Second edition, J. Ingber 28

Program Trace: pass by reference //Function Definition void swap(double& x, double& y) #include <iostream> using namespace std; { double temp; // void swap(double&, double&); //prototype temp = x; int main() { x=y; double x=5, y=10; y=temp; swap(x, y); //x y are arguments return; //optional cout << "x = " << x << ', ' }//end swap << " y= " << y << endl; return 0; } //end main Memory Snapshot: main() swap() double x double y double temp ? 5 double x 10 double y 10 Engineering Problem Solving with C++, Second edition, J. Ingber 29

Program Trace: pass by reference //Function Definition void swap(double& x, double& y) #include <iostream> using namespace std; { double temp; // void swap(double&, double&); //prototype temp = x; int main() { x=y; double x=5, y=10; y=temp; swap(x, y); //x y are arguments return; //optional cout << "x = " << x << ', ' }//end swap << " y= " << y << endl; return 0; } //end main Memory Snapshot: main() swap() double x double y double temp ? 5 double x 10 double y 5 return; Engineering Problem Solving with C++, Second edition, J. Ingber 30

Program Trace: pass by reference //Function Definition void swap(double& x, double& y) #include <iostream> using namespace std; { double temp; // void swap(double&, double&); //prototype temp = x; int main() { x=y; double x=5, y=10; y=temp; swap(x, y); //x y are arguments return; //optional cout << "x = " << x << ', ' }//end swap << " y= " << y << endl; return 0; } //end main Memory Snapshot: main() double x 10 double y 5 Arguments have been modified Engineering Problem Solving with C++, Second edition, J. Ingber 31

Practice! - What is the output? #include <iostream> using namespace std; void fun(int&, int); int main() { int c 1=1, c 2=2, c 3=3; cout << c 1 << ‘, ’ << c 2 << ‘, ’ << c 3 << endl; fun(c 1, c 2, c 3); cout << c 1 << ‘, ’ << c 2 << ‘, ’ << c 3 << endl; fun(c 3, c 2, c 1); cout << c 1 << ‘, ’ << c 2 << ‘, ’ << c 3 << endl; return 0; } void fun(int& a 1, int& a 2, int a 3) { a 1++; a 2++; a 3 --; Engineering Problem Solving with } C++, Second edition, J. Ingber 32

Storage Class and Scope 4 Scope refers to the portion of the program in which it is valid to reference a function or a variable 4 Storage class refers to the lifetime of a variable Engineering Problem Solving with C++, Second edition, J. Ingber 33

Scope 4 Local scope - a local variable is defined within a function or a block and can be accessed only within the function or block that defines it 4 Global scope - a global variable is defined outside the main function and can be accessed by any function within the program file. Engineering Problem Solving with C++, Second edition, J. Ingber 34

Storage Class - 4 types 4 automatic - key word auto - default for local variables – Memory set aside for local variables is not reserved when the block in which the local variable was defined is exited. 4 external - key word extern - default for global variables – Memory is reserved for a global variable throughout the execution life of the program. 4 static - key word static – Requests that memory for a local variable be reserved throughout the execution life of the program. The static storage class does not affect the scope of the variable. 4 register - key word register – Requests that a variable should be placed in a high speed memory Engineering register. Problem Solving with 35 C++, Second edition, J. Ingber

//Scope Example - What is the output #include <iostream> using namespace std; void a(); //Function prototypes void b(); int x=1; //Global (file scope) int main() { int x=5; //Local to main cout << "before call to a, x= " void a() << x << endl; { int x=25; //Local to a cout << "global x is " << : : x << endl; x++; a(); } cout << "after call to a, x= " void b() << x << endl; { cout << "global x is " << : : x << endl; x++; b(); } cout << "after call to b, x= " << x << endl; cout << "global x is " << : : x << endl; b(); cout << "after 2 nd call to b, x= " << x << endl; cout << "global x is " << : : x << endl; Engineering Problem Solving with 36 return(0); C++, Second edition, J. Ingber }

Scope Example - Output before call to a, x= 5 global x is 1 after call to b, x= 5 global x is 2 after 2 nd call to b, x= 5 global x is 3 Engineering Problem Solving with C++, Second edition, J. Ingber 37

//Storage Class Example - What is the output #include <iostream> using namespace std; void donothing(); int main() { donothing(); return(0); }//endmain void donothing() { int x=0; static int y=0; x++; y++; cout <<“x is ” x << “, y is ” << y << endl; return; }//end donothing Engineering Problem Solving with C++, Second edition, J. Ingber 38

Storage Class Example - Output x is 1, y is 1 x is 1, y is 2 x is 1, y is 3 Engineering Problem Solving with C++, Second edition, J. Ingber 39
- Slides: 39