Passing Arguments To Function in CPP - Docx-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Passing arguments to function in C++

In C++ we can pass arguments into a function in different ways. These different ways are −
● Call by Value
● Call by Reference

Call by Value
● In call by value, the actual value that is passed as argument is not changed after performing some
operation on it.

● When call by value is used, it creates a copy of that variable into the stack section in memory.

● When the value is changed, it changes the value of that copy, the actual value remains the
same.

#include<iostream>
using namespace std;

void my_function(int x)
{
x = 50;
cout << "Value of x from my_function: " << x << endl;
}

main()
{
int x = 10;
my_function(x);
cout << "Value of x from main function: " << x;
}
Output
Value of x from my_function: 50
Value of x from main function: 10

Call by Reference
● In call by reference the actual value that is passed as argument is changed after performing some
operation on it.
● When call by reference is used, it creates a copy of the reference of that variable into the stack
section in memory. Is uses a reference to get the value.
● So when the value is changed using the reference it changes the value of the actual variable.
#include<iostream>
using namespace std;

void my_function(int &x) {


x = 50;
cout << "Value of x from my_function: " << x << endl;
}

main() {
int x = 10;
my_function(x);
cout << "Value of x from main function: " << x;
}
Output
Value of x from my_function: 50
Value of x from main function: 50
Example 2 :
#include<iostream>
using namespace std;

void swapnum(int &i, int &j)


{
int temp = i;
i = j;
j = temp;
}

int main(void)
{
int a = 10;
int b = 20;

swapnum(a, b);
cout<<"\nValue of A is "<<a;
cout<<"\nValue of B is "<<b;
return 0;
}
Output :
Value of A is 20
Value of B is 10

Default Argument in C++

● While writing functions in C++ programming that take arguments as function parameters, we can
assign default values to those arguments during the function declaration.
● These default values assigned to the function parameters are known as default arguments in C++.
● If the function requiring parameters gets called without any arguments, it uses the default
arguments, which are assigned automatically by the compiler at the time of compilation.
● However, the default arguments get ignored when we call the function by passing the required
arguments.
● The values associated with the default arguments in C++ are not constant.
● We can use these values only when the function gets called without the appropriate arguments.
● Otherwise, the declared values get overwritten by the passed values.
● Example :
// CPP Program to demonstrate Default Arguments
#include <iostream>
using namespace std;

int sum(int x, int y, int z = 4, int w = 0) //assigning default values to z,was 0


{
return (x + y + z + w);
}

int main()
{

cout << sum(10, 15) << endl;


cout << sum(10, 15, 25) << endl;
cout << sum(10, 15, 25, 30) << endl;
return 0;
}

Object as function argument

● The objects of a class can be passed as arguments to member functions as well as nonmember
functions either
1. by value
2. by reference.
● When an object is passed by value, a copy of the actual object is created inside the function.
This copy is destroyed when the function terminates.
● Moreover, any changes made to the copy of the object inside the function are not reflected in the
actual object
● On the other hand, in pass by reference, only a reference to that object (not the entire object) is
passed to the function. Thus, the changes made to the object within the function are also
reflected in the actual object.
● Whenever an object of a class is passed to a member function of the same class, its data
members can be accessed inside the function using the object name and the dot operator.
● However, the data members of the calling object can be directly accessed inside the function
without using the object name and the dot operator.
Pass by Value example1:
Sample program to demonstrate concept of Object as function argument
#include<iostream>
using namespace std;
class weight
{
int kilogram;
int gram;
public:
void getdata ();
void putdata ();
void sum_weight (weight,weight) ;
};

void weight :: getdata()


{
cout<<"/nKilograms:";
cin>>kilogram;
cout<<"Grams:";
cin>>gram;
}
void weight :: putdata ()
{
cout<<kilogram<<" Kgs. and"<<gram<<" gros.\n";
}

void weight :: sum_weight(weight w1,weight w2)


{
gram = w1.gram + w2.gram;
kilogram=gram/1000;
gram=gram%1000;
kilogram+=w1.kilogram+w2.kilogram;
}

int main ()
{
weight w1,w2,w3;
cout<<"Enter weight in kilograms and grams\n";
cout<<"\n Enter weight #1" ;
w1.getdata();
cout<<" \n Enter weight #2" ;
w2.getdata();
w3.sum_weight(wl,w2);
cout<<"/n Weight #1 = ";
w1.putdata();
cout<<"Weight #2 = ";
w2.putdata();
cout<<"Total Weight = ";
w3.putdata();
return 0;
}

The output of the program is


Enter weight in kilograms and grams
Enter weight #1
Kilograms: 12
Grams: 560
Enter weight #2
Kilograms: 24
Grams: 850
Weight #1 = 12 Kgs. and 560 gms.
Weight #2 = 24 Kgs. and 850 gms.
Total Weight = 37 Kgs. and 410 gms.
Pass by Value example 2:
/* C++ program to Add two Complex number passing objects to function */

#include<iostream>
using namespace std;

class complex
{
int re,im;
public:
void get()
{
cout<<"\nEnter Real Part :: ";
cin>>re;
cout<<"\nEnter Imag. Part :: ";
cin>>im;
}

void disp()
{
cout<<re<<"+"<<im<<"i"<<"\n";

}
void sum(complex,complex);
};

void complex::sum(complex c1,complex c2)


{
re=c1.re+c2.re;
im=c1.im+c2.im;
}

int main()
{
complex c1,c2,c3;
cout<<"Enter 1st complex no.: \n";
c1.get();
cout<<"\nEnter 2nd complex no.: \n";
c2.get();
cout<<"\nThe 1st complex no. is :: ";
c1.disp();
cout<<"\nThe 2nd complex no. is :: ";
c2.disp();

c3.sum(c1,c2);

cout<<"\nThe Sum of two complex no.s are :: ";


c3.disp();

return 0;

}
Output :
Enter 1st complex no.:

Enter Real Part :: 1

Enter Imag. Part :: 2

Enter 2nd complex no.:

Enter Real Part :: 3

Enter Imag. Part :: 4

The 1st complex no. is :: 1+2i


The 2nd complex no. is :: 3+4i

The Sum of two complex no.s are :: 4+6i

Object af function Argument (Pass by reference example)


//WAP to swap two numbers using passing object as function arguments by reference.
#include<iostream>
#include<conio.h>
using namespace std;
class temp
{
int x, y, q;
public:
void input()
{
cout << "Enter Two Numbers :";
cin >> x>>y;
}
void display()
{
cout << "After Swap x is :" << x;
cout << "After Swap y is :" << y;
}
void swap(temp &t)
{
t.q = t.x;
t.x = t.y;
t.y = t.q;
}

};

int main()
{
temp t1;
t1.input();
t1.swap(t1);
t1.display();
return 0;
}

Friend Function

FRIEND FUNCTIONS:
● A non-member function cannot have an access to the private data of a class.
● It is partially proved in some situations where common function is needed in more than one
classes.
● In such cases common function can be define which not member of any class is, but it can be
made friendly with all classes and thus allowing that function to have access to the private data of
these classes.
● To make outside function friendly to a class that function should be declare with keyword
‘friend’ and that function should be define anywhere in a program (not within class) like a
normal function.

Friend function have special characteristics as:


1) It is not in scope of class to which it has been declare as friend.
2) It cannot be called using object of that class. Rather it can be called like normal function without
any object.
3) It cannot access members of class directly and has to use object name and dot operator. (e.g.
object data)
4) It can be declare either in private or public section of class.
5) Usually it has object as argument.

Following example illustrate use of friend function:


WAP to create a class SYCO having data members as roll_no and name. Accept this data and display
for one object of class. Make putdata function s friend function.
class SYCO
{ int roll_no;
char name [20];
public :
void getdata ( );
friend void putdata (SYCO); // function declaration as friend function
} S1;

void SYCO :: getdata ( )


{
cout << “enter the roll no and name ”;
cin >> roll no >> name;
}

void putdata (SYCO S1) // function definition without class name and ::
{
cout << “roll_no “<< S1.roll_no; //object used to access data variables
cout << name “<< S1. name;
}

void main ( )
{ S1. getdata ( );
putdata (SYCO S1); // call to friend function. call without object
}

WAP for swapping contents of two variables of same class using friend function

#include<iostream.h>
#include<conio.h>
using namespace std;
class temp
{
int x, y, q;
public:
void input()
{
cout << "Enter Two Numbers :";
cin >> x>>y;
}
void display()
{
cout << "After Swap x is :" << x;
cout << "After Swap y is :" << y;
}
friend void swap(temp &t);

};
void swap(temp &t)
{
t.q = t.x;
t.x = t.y;
t.y = t.q;
}
int main()
{
temp t1;
t1.input();
swap(t1);
t1.display();
return 0;
}

You might also like