Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.

Similar presentations


Presentation on theme: "1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006."— Presentation transcript:

1 1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006

2 2 Parameters Passing Pass by value (A formal parameter is a value parameter): The value of the corresponding actual parameter is copied into it The value parameter has its own copy of the data Any modifications to the local copy do not change the original variable in the calling program Pass by reference (A formal parameter is a reference parameter): An alias of the argument is passed to the called function. no copies of the actual parameter are made. When references are passed into a function, any changes made to the references will be seen in the calling function.

3 3 #include using namespace std void swap(int x, int y); int main() { int x = 4; int y = 2; cout << "Before swap, x is " << x << ", y is " << y << endl; swap(x,y); cout << "After swap, x is " << x << ", y is " << y << endl; } void swap(int first, int second) { int temp; temp = second; second = first; first = temp; } A formal parameter receives a copy of the content of corresponding actual parameter. which is called Pass by value & & & & Any modifications to the local copy do not change the original variable in the calling program. An alias of the argument is passed to the called function. which is called Pass by reference When references are passed into a function, any changes made to the references will be seen in the calling function. Parameters Passing

4 4 #include using namespace std; void addFirst(int& first, int& second); void doubleFirst(int one, int two); void squareFirst(int& ref, int val); int main () { int num = 5; addFirst(num, num); doubleFirst(num, num); squareFirst(num, num); return 0; } void addFirst(int& first, int& second) { first = first + 2; second = second * 2; } void doubleFirst(int one, int two) { one = one * 2; two = two + 2; } void squareFirst(int& ref, int val) { ref = ref * ref; val = val + 2; } // After this statement, num=5 //Now first=5, second=5 //After this statement, first=7, second=7 // After this statement, first=14, second=14 // After this statement, num=14 //Now one=14, two=14 //After this statement, one=28, two=14 //After this statement, one=28, two=16 // After this statement, num=14 //Now ref=14, val=14 //After this statement, ref=196, val=14 //After this statement, ref=196, val=16 // After this statement, num=196 //first and second are reference parameters //first, second and num refer to the same object //ref is reference parameters //ref and num refer to the same object

5 5 Reference Variables as Parameters  Reference parameters can:  Pass one or more values from a function  Change the value of the actual parameter  Reference parameters are useful in three situations:  Returning more than one value  Changing the actual parameter  When passing the address would save memory space and time

6 6 Scope of an Identifier The scope of an identifier refers to where in the program an identifier is accessible Local identifier - identifiers declared within a function (or block) Global identifier – identifiers declared outside of every function definition C++ does not allow nested functions The definition of one function cannot be included in the body of another function

7 7 #include using namespace std; void f(); int main() { int x=1; f(); } void f() { cout<<“x=”<<x; } Error: x in function f() is not declared. Output : x=2 Try 1 #include using namespace std; void f(); int main() { int x=1; f(); } void f() { int x=2; cout<<“x=”<<x; } Try 2 #include using namespace std; int x=1; void f(); int main() { f(); } void f() { cout<<“x=”<<x; } Try 3 Output : x=1 #include using namespace std; void f(int num); int main() { int x=1; f(x); } void f(int num) { cout<<“x=”<<num; } Try 4 Output : x=1 Local identifier Global identifier Local identifier

8 8 Scope of an Identifier (continued) Global identifiers (such as variables) are accessible by a function or a block if The identifier is declared before the function definition (block) The function name is different from the identifier All parameters of the function have names different than the name of the identifier All local identifiers (such as local variables) have names different than the name of the identifier

9 9 #include using namespace std; void f() { cout<<"x="<<x; } int x=1; int main() { f(); } Error: x in function f() is not declared. Error: 'x' redefinition; previous definition was 'function' Case 1 #include using namespace std; void x(); int x=1; int main() { x(); } void x() { cout<<"x="<<x; } Case 2 #include using namespace std; int x=1; void f(int x); int main() { int num=2; f(num); } void f(int x) { cout<< "x="<<x; } Case 3 Output : x=2 #include using namespace std; void f(); int x=1; int main() { f(); } void f() { int x=2; cout<< "x="<< x; } Case 4 Output : x=2 The global identifier should be declared before the function definition The global identifier x can not be accessed by f() The global identifier x can not be accessed by function x() The function name should be different from the global identifier The global identifier x can not be accessed by function f() All parameters of the function should have names different than the name of the global identifier The global identifier x can not be accessed by function f() All local identifiers (such as local variables) should have names different than the name of the global identifier

10 10 Scope of an Identifier (continued) An identifier declared within a block (Nested Block) is accessible: Only within the block from the point it is declared until the end of the block By those blocks that are nested within that block if the nested block does not have an identifier with the same name as that of the outside block (the block that encloses the nested block) The scope of a function name is similar to the scope of an identifier declared outside of any block

11 11 #include using namespace std; void f(); int x=1; int main() { int x=2; { int x=3; { int x=4; { int x=5; cout<<"Line 1: "<<x<<endl; } cout<<"Line 2: "<<x<<endl; } cout<<"Line 3: "<<x<<endl; } cout<<"Line 4: "<<x<<endl; f(); } void f() { cout<<"Line 5: "<<x<<endl; } Output: Line 1: 5 Line 2: 4 Line 3: 3 Line 4: 2 Line 5: 1

12 12 Global Variables Some compilers initialize global variables to default values The operator :: is called the scope resolution operator By using the scope resolution operator A global variable declared before the definition of a function (block) can be accessed by the function (or block) even if the function (or block) has an identifier with the same name as the variable C++ provides a way to access a global variable declared after the definition of a function extern int w; In this case, the function must not contain any identifier with the same name as the global variable

13 13 #include using namespace std; void f(); int x=1; int main() { f(); } void f() { int x=2; cout<< "x="<< x; } Case 4 Output : x=2 The global identifier x can not be accessed by function f() All local identifiers (such as local variables) should have names different than the name of the global identifier #include using namespace std; void f(); int x=1; int main() { f(); } void f() { int x=2; cout<< "x="<<::x; } Output: x=1

14 14 #include using namespace std; void f() { cout<<"x="<<x; } int x=1; int main() { f(); } Error: x in function f() is not declared. Case 1 The global identifier should be declared before the function definition The global identifier x can not be accessed by f() #include using namespace std; void f() { extern int x; cout<<"x="<<x; } int x=1; int main() { f(); } Output: x=1

15 15 Side Effects of Global Variables Using global variables has side effects Any function that uses global variables Is not independent Usually it cannot be used in more than one program If more than one function uses the same global variable and something goes wrong It is difficult to find what went wrong and where Problems caused by global variables in one area of a program might be misunderstood as problems caused in another area

16 16 Static and Automatic Variables Automatic variable - memory is allocated at block entry and deallocated at block exit Static variable - memory remains allocated as long as the program executes Variables declared outside of any block are static variables By default variables declared within a block are automatic variables

17 17 Static and Automatic Variables (continued) Declare a static variable within a block by using the reserved word static The syntax for declaring a static variable is: static dataType identifier; The statement static int x; declares x to be a static variable of the type int Static variables declared within a block are local to the block Their scope is the same as any other local identifier of that block

18 18 Output: x=2 y=11 x=4 y=11 x=6 y=11 x=8 y=11 x=10 y=11 //Program: Static and automatic variables #include using namespace std; void test(); int main() { int count; for (count = 1; count <= 5; count++) test(); return 0; } void test() { static int x = 0; int y = 10; x = x + 2; y = y + 1; cout << "x = " << x <<endl; cout << " y = " << y << endl; }

19 19 End of lecture 18 Thank you!


Download ppt "1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006."

Similar presentations


Ads by Google