70 likes | 191 Views
Object Oriented Programming in C++. LECTURE 10. ARGUMENTS PASS TO THE FUNCTION. Arguments can be sent to function either by value , where the function works with the copy of the arguments, or by reference , where the function works with the original arguments in the calling program.
E N D
Object Oriented Programming in C++ LECTURE 10
ARGUMENTS PASS TO THE FUNCTION • Arguments can be sent to function either by value, where the function works with the copy of the arguments, or by reference, where the function works with the original arguments in the calling program. • Reference Arguments:A reference provides an alias---- a different name---- for a variable. One of the most important uses for references is in passing arguments to functions. • Passing arguments by reference uses a different mechanism. Instead of value being passed to the function, a reference to the original variable, in the calling program, is passed. (its actually the memory address of the variable that is passed). • An important advantage of passing by reference is that the function can access the actual variable 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. www.itcafe.741.com
When a variable is passed by reference we are not passing a copy of its value, but we are somehow passing the variable itself to the function and any modification that we do to the local variables will have an effect in their counterpart variables passed as arguments in the call to the function. • Void function-name(int& a, int& b); function-name( x, y); • To explain it in another way, we associate a, and b with the arguments passed on the function call (x, and y ) and any change that we do on “a” within the function will affect the value of “x” outside it. Any change that we do on b will affect y. www.itcafe.741.com
Example void intfract(float n, float& intp, float& fracp) { int x= (int)n; intp=(float)x; fracp=n-intp; } • #include<constream.h> • void intfract(float, float&, float&); • void main() • { • float num,intpart,fracpart; • do • { • cout<<"Enter a real number "; • cin>>num; • intfract(num,intpart,fracpart); • cout<<"\n Integer part is "<<intpart<<"\n Fractional Part is "<<fracpart<<endl; • }while(num!=0.0); • getch(); • } www.itcafe.741.com
The main part of the program asks the user to enter a real number. The program will separate this number into integer and fractional part. The output of the program is as follow: • Enter a real number 99.44 • Integer part is 99 fractional part is 0.44. • Some compiler may generate spurious digits in the fractional part, such as 0.440002. this is an error in the compiler’s conversion routines and can be ignored. • This function finds an integer number by converting the real number with cast, and for obtaining the fractional part of the number its simply subtract the integer part from real number. int x= (int)n; fracp=n-intp • The intfract() function can find the integer and fractional parts, but how does it pass them back to main()? The problem is solved using reference arguments. www.itcafe.741.com
The declaration of the function is as: void intfrac(float n, float& intp, float& fracp) Reference arguments are indicated by the ampersand (&) following the data type. The & indicates that intp is an alias--- another name--- for whatever the variable is passed as an arguments. In other words, when you use the name intp in intfrac() function, you are really referring to intpart in main(). The & can be taken to mean reference to, so float&intp means intp is a reference to the float variable passed to it. Similarly, fracp is an alias for ---- or a reference to ---- fractpart. The ampersand is not used in the function call. www.itcafe.741.com
#include<constream.h> • void addition(int&, int&, int&); • void main() • { • int sum=0, x=10,y=20; • clrscr(); • sum=x+y; • cout<<"Addition of "<<x<<" and "<<y<<" is "<<sum<<endl; • x=5,y=3; • addition(sum,x,y); • cout<<"Addition of "<<x<<" and "<<y<<" is "<<sum; • getch(); • } • void addition(int& s, int& a, int& b) • { • s=a+b; • } www.itcafe.741.com