1 / 19

Lecture 18:User-Definded function II(cont.)

Lecture 18:User-Definded function II(cont.). Introduction to Computer Science Spring 2006. 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

moses
Download Presentation

Lecture 18:User-Definded function II(cont.)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. & & & & Parameters Passing #include <iostream>using namespace stdvoid 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.

  4. #include <iostream> 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 // After this statement, num=14 // After this statement, num=14 // After this statement, num=196 //Now first=5, second=5 //first and second are reference parameters //first, second and num refer to the sameobject //After this statement, first=7, second=7 // After this statement, first=14, second=14 //Now one=14, two=14 //After this statement, one=28, two=14 //After this statement, one=28, two=16 //ref is reference parameters //ref and num refer to the sameobject //Now ref=14, val=14 //After this statement, ref=196, val=14 //After this statement, ref=196, val=16

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

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

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

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

  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. 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. 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. //Program: Static and automatic variables #include <iostream> 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; } Output: x=2 y=11 x=4 y=11 x=6 y=11 x=8 y=11 x=10 y=11

  19. End of lecture 18 Thank you!

More Related