Unit2 Notes
Unit2 Notes
Parameters are specified after the function name, inside the parentheses. You
can add as many parameters as you want, just separate them with a comma:
Syntax
void functionName(parameter1, parameter2, parameter3) {
// code to be executed
}
The following example has a function that takes a string called fname as
parameter. When the function is called, we pass along a first name, which is
used inside the function to print the city name:
#include <iostream>
#include <string>
}
int main() {
myFunction("Pune");
myFunction("Mumbai");
myFunction("Nagpur");
return 0;
City name=Pune
City name=Mumbai
City name=Nagpur
we can provide default values for function parameters. i.e. we can assign
some value to the parameters in function definition in it’s brackets itself.
If a function with default arguments is called without passing arguments, then
the default parameters are used.
However, if arguments are passed while calling the function, the default
arguments are ignored.
o The values passed in the default arguments are not constant. These values can be
overwritten if the value is passed to the function. If not, the previously declared
value retains.
o During the calling of function, the values are copied from left to right.
o All the values that will be given default value will be on the right.
Example
Code
#include<iostream>
using namespace std;
int sum(int x, int y, int z=0, int w=0) // Here there are two values in the default arguments
{ // Both z and w are initialised to zero
return (x + y + z + w); // return sum of all parameter values
}
int main()
{
cout << sum(10, 15) << endl; // x = 10, y = 15, z = 0, w = 0
cout << sum(10, 15, 25) << endl; // x = 10, y = 15, z = 25, w = 0
cout << sum(10, 15, 25, 30) << endl; // x = 10, y = 15, z = 25, w = 30
return 0;
}
Output:
25
50
80
Explanation
In the above program, we have called the sum function three times.
o Sum(10,15)
When this function is called, it reaches the definition of the sum. There it initializes
x to 10 y to 15, and the rest values are zero by default as no value is passed. And
all the values after sum give 25 as output.
o Sum(10,15,25)
When this function is called, x remains 10, y remains 15, the third parameter z that
is passed is initialized to 25 instead of zero. And the last value remains 0. The sum
of x, y, z, w, is 50 which is returned as output.
o Sum(10,15,25,30)
In this function call, there are four parameter values passed into the function with x as 10,
y as 15, z is 25, and w as 30. All the values are then summed up to give 80 as the output.
Like any other data type an object can be used as a function argument and
passed to function. This can be done in two ways :
1) A copy of entire object is passed to function (pass by value)
2) Only the address of object is transferred to function (pass by reference)
1) Pass by Value:
Objects may be passed to functions in just the same way that any other type of variable can.
Objects are passed to functions through the use of the standard callby value mechanism. This
means that a copy of an object is made when it is passed to a function. When a copy of an object
is generated because it is passed to a function, the object’s constructor function is not called.
However, when the copy of the object inside the function is destroyed, its destructor function is
called. Here, any changes made to object inside the function do not affect the object used to call
the function.
Following program :
It illustrates use of object as function arguments. It performs addition of two numbers.
class add
{int x, Result,
public :
int getdata();
void sum (add, add);
} a1, a2, a3;
int add :: getdata()
{
cout << “Enter your No =”;
cin >> x;
return o;
}
void add :: sum (add a1, add a2)
{ Result = a1.x + a2.x;
count << “addition =” << Result;
}
void main()
{ a1.getdata();
a2.getdata();
a3.sum (a1, a2); // call to function, a1 and a2 objects are passed to function.
getch();
}
2) Pass by Reference:
When an address of object is passed as a argument to a function it is called as
passed by reference. In this type the called function works directly on actual objects
used in call. It means any changes made to the object inside function will reflect in
actual object. This method can be used to alter value of private members of a class.
Example : Program to do addition of two numbers where objects are passed
by reference to the function SUM. (Same above program is converted into object Pass
by Reference).
# include < iostream.h>
# include < conio.h>
class add
{ int x, result;
public :
int getdata ( )
void SUM (add & add & );
} a1, a2, a3;
Example: In the above example we can see that the add function does not return any value since its
return-type is void. In the following program the add function returns an object of type ‘Example'(i.e.,
class name) whose value is stored in E3.
In this example, we can see both the things that are how we can pass the objects as well as return them.
When the object E3 calls the add function it passes the other two objects namely E1 & E2 as arguments.
Inside the function, another object is declared which calculates the sum of all the three variables and
returns it to E3.
This code and the above code is almost the same, the only difference is that this time the add function
returns an object whose value is stored in another object of the same class ‘Example’ E3. Here the value
of E1 is displayed by object1, the value of E2 by object2 and value of E3 by object3.
// of objects to a function
#include <bits/stdc++.h>
class Example {
public:
int a;
// return object
Example add(Example Ea, Example Eb)
Example Ec;
return Ec;
};
int main()
E1.a = 50;
E2.a = 100;
E3.a = 0;
<< "\n";
// to function add()
E3 = E3.add(E1, E2);
<< "\n";
return 0;
Output:
Initial Values
object 2: 100,
object 3: 0
New values
object 2: 100,
object 3: 200
void putdata (SYIT 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 (SYIT S1); // call to friend function. call without object
}