Download presentation
Presentation is loading. Please wait.
Published byLambert Mitchell Modified over 9 years ago
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
2
PARAMETER PASSING pass by value pass by reference storage class and scope Engineering Problem Solving with C++, Second edition, J. Ingber 2
3
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
4
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
5
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.
6
Engineering Problem Solving with C++, Second edition, J. Ingber 6 //Function Definition int 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 if return 0; }//end main
7
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 header void 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.
8
Pass-By-Reference Example scale.cpp
9
Engineering Problem Solving with C++, Second edition, J. Ingber 9 Example - pass by reference #include using namespace std; //Function swap interchanges the values of two variables //function definition void swap(double& a, double& b)//function header { double temp;//local variable temp temp = a; a=b; b=temp; return;//optional return statement }//end swap
10
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?
11
Engineering Problem Solving with C++, Second edition, J. Ingber 11 Practice! - What is the output? #include 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; }
12
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. Block A sequence of statements enclosed in {} that introduces a new scope
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.
14
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.
15
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.
16
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.
17
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.