engineering problem solving with c++, second edition, j. ingber 1 engineering problem solving with...

17
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

Upload: lambert-mitchell

Post on 17-Jan-2016

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

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

1

Engineering Problem Solving with C++, Etter/Ingber

Chapter 5

Parameter Passing

11/06/13

Page 2: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

PARAMETER PASSING

pass by value

pass by reference

storage class and scope

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

2

Page 3: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

Parameter Passing

C++ supports two forms of parameter passing:– Pass by value.– Pass by reference.

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

3

Page 4: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

Parameter Passing

C++ supports two forms of parameter passing:– Pass by value.– Pass by reference.

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

4

Page 5: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

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

5

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.

Page 6: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

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

6

//Function Definitionint fact(int num)//4{ int nfact = 1;//5 while(num>1) { nfact = nfact*num; num--; } return(nfact);//6} //end fact

int main(){ int n, factorial;//1 cin >> n; //2 if(n>=0) { factorial = fact(n);//3 cout << n <<"! is " << factorial << endl;//7 }//end ifreturn 0;}//end main

Page 7: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

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

7

Parameter Passing - pass by reference

Pass by reference allows modification of a function argument.

Must append an & to the parameter data type in both the function prototype and function headervoid getDate(int& day, int& mo, int& year)

Formal parameter receives the address of the argument.

Any changes to the formal parameter directly change the value of the argument.

Page 8: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

Pass-By-Reference Example

scale.cpp

Page 9: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

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

9

Example - pass by reference

#include <iostream>using namespace std;

//Function swap interchanges the values of two variables//function definitionvoid swap(double& a, double& b) //function header{

double temp; //local variable temptemp = a;a=b;b=temp;return; //optional return statement

}//end swap

Page 10: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

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

10

Another Example - pass by reference

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?

Page 11: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

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

11

Practice! - What is the output?

#include <iostream>using namespace std;void fun(int& a1, int& a2, int a3){ a1++;

a2++;a3--;

}

int main(){ int c1=1, c2=2, c3=3;

cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl;

fun(c1,c2,c3);cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl;

fun(c3, c2, c1);cout << c1 << ‘,’ << c2 << ‘,’ << c3 << endl; return 0;

}

Page 12: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

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

12

Scope

Scope refers to the portion of the program in which it is valid to reference a function or a variable.

BlockA sequence of statements enclosed in {} that introduces

a new scope

Page 13: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

13

Scope

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

Global scope - a global variable is defined outside the any function and can be accessed by any function within the program file.

Page 14: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

14

Scope

Ch5/scope1.cpp - Global PI and local variables belonging to the functions

Ch5/scope2.cpp - Global variable referred to in main and other function.

Ch5/scope3.cpp - Local variable declared within an if block.

Page 15: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

15

Questions

Use pass-by-______________ when a change in the argument is desired.

The_________ of a name is the portion of the program in which the name can be used.

A variable declared outside of any block is called a _________ variable.

Page 16: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

Write a Midpoint Function

Write a C++ function to find the midpoint of a line segment in a Cartesian plane, given the coordinates of the endpoints of the segment.

Page 17: Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13

Write normalize( )

Write a C++ function to take the numerator and denominator of a fraction. It should normalize the fraction. Change the fraction so that the denominator is a positive number.