Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.

Similar presentations


Presentation on theme: "Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables."— Presentation transcript:

1 Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables

2 Methods of Passing  There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference,  and pass by address. ( will be addressed later)

3 Methods of Passing  When passing arguments by value, the only way to return a value back to the caller is via the function’s return value.  While this is suitable in many cases, there are a few cases where better options are available.

4 Methods of Passing  When arguments are passed by value, the called function creates a new variable of the same type as the argument and copies the argument’s value into it. As we noted, the function cannot access the original variable in the calling program, only the copy it created.  Passing arguments by value is useful when the function does not need to modify the original variable in the calling program.  In fact, it offers insurance that the function cannot harm the original variable.

5 Passing Arguments by Reference  Passing arguments by reference uses a different mechanism. Instead of a value being passed to the function, a reference to the original variable, in the calling program, is passed.  An important advantage of passing by reference is that the function can access the actual variables in the calling program.  This provides a mechanism for passing more than one value from the function back to the calling program.

6 Passing Arguments by Reference  In pass by reference, we declare the function parameters as references rather than normal variables: #include Using namespace std ; Void duplicate (int& a, int& b, int & c); Int main () { Int x=1,y=3,z=7; Duplicate (x,y,z); Cout << “x=“<<x<<“, y=“<<y<<“,z=“<<z; Return 0; } Void duplicate (int& a, int& b, int & c) {a*=2; b*=2; c*=2; }

7 Passing Arguments by Reference Reference arguments are indicated by the ampersand (&) following the data type: int& a When the function is called, a will become a reference to the argument. Since a reference to a variable is treated exactly the same as the variable itself, then any changes made to the reference are passed through to the argument!

8 Passing Arguments by Reference  Sometimes we need a function to return multiple values.  However, functions can only have one return value.  One way to return multiple values is using reference parameters.

9 Advantages of passing by reference  It allows us to have the function change the value of the argument, which is sometimes useful.  Because a copy of the argument is not made, it is fast, even when used with large structs or classes.  We can pass by const reference to avoid unintentional changes.  We can return multiple values from a function.

10 Recursion and Recursive Functions  Main calls another function…..normal  A function calls another function2….normal  A function calls itself ?! Possible?? YES  A recursive function is one that call itself.

11 Recursion and Recursive Functions  An example to print numbers counting down: void print (int p) { if (p==0) return; cout<<p; print(p-1); return; }

12 Finding Factorial Recursively 5! 5*4! 4*3! 3*2! 2*1! 1 1 5! 5*4! 4*3! 3*2! 2*1! 1 1 Final value=120 1 2!=2*1=2 returned 3!=3*2=6 returned 4!=4*6=24 returned 5!=5*24=120 returned

13 Finding Factorial Recursively //Recursive factorial Function #include unsigned long factorial(unsigned long);//prototype int main() { int num; cout<<“enter a positive integer:”; cin>>num; cout<<“factorial=“<<factorial(num); return 0; } unsigned long factorial(unsigned long n) { if ( n <= 1) //the base case return 1; else return n * factorial (n - 1); } //Recursive factorial Function #include unsigned long factorial(unsigned long);//prototype int main() { int num; cout<<“enter a positive integer:”; cin>>num; cout<<“factorial=“<<factorial(num); return 0; } unsigned long factorial(unsigned long n) { if ( n <= 1) //the base case return 1; else return n * factorial (n - 1); }

14 Function Overloading  Functions with same name and different parameters or return data type  Should perform similar tasks  I.e., function to square ints and function to square floats int square( int x) {return x * x;} float square(float x) { return x * x; }  A call-time c++ complier selects the proper function by examining the number, type and order of the parameters

15 Function Overloading  An overloaded function appears to perform different activities depending on the kind of data sent to it.  It performs one operation on one kind of data but another operation on a different kind.

16 Function Overloading #include Using namespace std; Void repchar();//declarations Void repchar(char); Void repchar(char, int); intmain() { repchar(); //First repchar(‘=’); //Second repchar(‘+’,30); //Third Return 0; }

17 Void repchar() // first { For (int j=0; j<45; j++) cout<<‘*’; cout<<endl; } Void repchar (char ch) // Second { For (int j=0;j<45; j++) cout<<ch; cout<<endl; } Void repchar(char ch,int n) //Third { for(int j=0;j<n; j++) cout<<ch; cout<<endl; }

18 Function Overloading This program prints out three lines of characters. Here’s the output: ********************************************* ============================================= ++++++++++++++++++++++++++++++

19 Function Overloading  The program contains three functions with the same name.  There are three declarations, three function calls, and three function definitions.  The compiler uses the function signature—the number of arguments, and their data types—to distinguish one function from another.

20 Function Overloading  voidrepchar();  which takes no arguments, describes an entirely different function than the declaration  voidrepchar(char);  which takes one argument of type char, or the declaration  voidrepchar(char, int);  which takes one argument of type char and another of type int.

21 Global and Local Variables  A variable’s scope, also called visibility, describes the locations within a program from which it can be accessed.  The scope of a variable determines which parts of the program can access it.  Scope: Local and Global  Variables defined within a function body are called local variables because they have local scope. voidsomefunc() { int somevar;//variablesdefinedwithin float othervar;//thefunctionbody }

22 Global and Local Variables  Variables defined within a function are only visible, meaning they can only be accessed, from within the function in which they are defined. Void somefunc() { intsomevar;//localvariables float othervar; somevar=10;//OK othervar=11;//OK nextvar=12;//illegal:notvisibleinsomefunc() } void otherfunc() { intnextvar; //localvariable somevar=20;//illegal:notvisibleinotherfunc() othervar=21;//illegal:notvisibleinotherfunc() nextvar=22;//OK }

23 Global and Local Variables  A global variable is used when it must be accessible to more than one function in a program.  While local variables are defined within functions, global variables are defined outside of any function.  Global variable is visible to all those functions that follow the variable’s definition in the listing.

24 Global and Local Variables


Download ppt "Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables."

Similar presentations


Ads by Google