130 likes | 133 Views
Intro to Programming Week # 8 Functions II Lecture # 13. By: Saqib Rasheed. Parameters by value. When arguments are passed by value, the called function creates a new variable of the same type and copies the argument’s value into it.
E N D
Intro to ProgrammingWeek # 8Functions IILecture # 13 By: Saqib Rasheed
Parameters by value • When arguments are passed by value, the called function creates a new variable of the same type and copies the argument’s value into it. • 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. Saqib Rasheed
Parameters 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. • It’s actually pass the memory address of the variable that is passed • An important advantage of passing by reference is that the function can access the actual variables in the calling program. • Among other benefits, this provides a mechanism for passing more than one value from the function back to the calling program.
Example void foo(int &y) ; int main() { int x = 5; cout << "x = " << x ; foo(x); cout << "x = " << x ; return 0; } void foo(int &y) { cout << "y = " << y ; y = 6; cout << "y = " << y; } // y is destroyed here Saqib Rasheed
Passing By ValueVersusPassing By Reference Passing By reference • int &x; • The parameter x is a local reference. • It is a copy for the argument. • It can change the argument. • The argument passed by reference must be a variable. • The argument is read-write. Passing By Value • int x; • The parameter x is a local variable. • It is a duplicate of the argument. • It cannot change the argument. • The argument passed by value may be a constant, a variable, or an expression. • The argument is read-only. Saqib Rasheed
Passing values by reference e.g void square(double &); void main() { double x; cout <<"\n main(), before calling "; cout<<"\nEnter the number to find square="; cin>>x; square(x); cout <<"\n main(), after calling square(), x = " << x<<endl; } void square(double &x) { x=x*x; } Saqib Rasheed
Example Develop a program that swap the two values using function by reference i.e a = 22.2 b = 44.4 After swap a = 44.4 b = 22.2 Saqib Rasheed
Example(code) void swap(float ,float ); void main() { float a = 22.2,b = 44.4; cout << "a = " << a << ", b = " << b << endl; swap(a,b); cout << "a = " << a << ", b = " << b << endl; } void swap(float x, float y) { float temp = x; x = y; y = temp; } Saqib Rasheed
Example(code) void swap(float&,float&); void main() { float a = 22.2,b = 44.4; cout << "a = " << a << ", b = " << b << endl; swap(a,b); cout << "a = " << a << ", b = " << b << endl; } void swap(float& x, float& y) { float temp = x; x = y; y = temp; } Saqib Rasheed
Function Overloading int test() { } int test(int a){ } int test(double a){ } int test(int a, double b){ } • All 4 functions mentioned above are overloaded function. It should be noticed that, the return type of all 4 functions is same,i.e, int. • Overloaded function may or may not have different return type but it should have different argument Saqib Rasheed
Example void test(int var) { cout<<"Integer number: "<<var; } void test(float var){ cout<<"Float number: "<<varl; } void test(int var1, float var2) { cout<<"Integer number: "<<var1; cout<<" And float number:"<<var2; } void test(int); void test(float); void test(int, float); int main() { int a = 5; float b = 5.5; test(a); test(b); test(a, b); return 0; } Saqib Rasheed
Overloading the max() Function #include<iostream.h> int max(int,int); int max(int,int,int) ; int main() { int x,y,z; cout<<"Enter x ="; cin>>x; cout<<"Enter y ="; cin>>y; cout<<"Enter z ="; cin>>z; } Saqib Rasheed
Overloading the max() Function cout << "Largest Among Two = "<<max(x,y) << "\nLargest Among Three =" << max(x,y,z); cout<<endl; int max(int x,int y) { // returns the maximum of the two given integers: return (x > y ? x : y); } int max(int x,int y, int z) { // returns the maximum of the three given integers: int m = (x > y ? x : y); // m = max(x,y) return (z > m ? z : m); } Saqib Rasheed