0% found this document useful (0 votes)
6 views

Unit2 Notes

Uploaded by

aryanjadhav400
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit2 Notes

Uploaded by

aryanjadhav400
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Unit 02: Function in C++

Member Functions: Passing arguments to Functions: Passing constant/ Default arguments,


passing variables, passing object as an argument- passing by value and passing by reference,
nesting of member function Returning values from functions: The return statement, Returning by
reference, Returning object from function. Inline function, Friend Function.

Information can be passed to functions as a parameter. Parameters act as


variables inside the function.

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>

using namespace std;

void myFunction(string fname)

cout <<"\n City name="<<fname;

}
int main() {

myFunction("Pune");

myFunction("Mumbai");

myFunction("Nagpur");

return 0;

Output of the code:

City name=Pune

City name=Mumbai

City name=Nagpur

PASSING DEFAULT PARAMETERS TO FUNCTIONS:

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.

A default argument is a value in the function declaration automatically assigned by the


compiler if the calling function does not pass any value to that argument.

Characteristics for defining the default arguments

Following are the rules of declaring default arguments -

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

o void function(int x, int y, int z = 0)


Explanation - The above function is valid. Here z is the value that is predefined as
a part of the default argument.
o Void function(int x, int z = 0, int y)
Explanation - The above function is invalid. Here z is the value defined in between,
and it is not accepted.

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.

OBJECT AS FUNCTION ARGUMENTS:

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;

int add : : getdata ( )


{ cout << “enter your roll nos.”;
cin >> x;
return (o);
}

void add : : SUM (add & a1, add & a2)


{
Result = a1x + a2x
cout << “addition = “<< Result;
}
main ( )
{
a1. getdata ( );
a2. getdata ( );
a3.SUM (a1, a2)
getch ( );
}

RETURNING OBJECT AS ARGUMENT


Syntax:

object = return object_name;

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.

// C++ program to show passing

// of objects to a function

#include <bits/stdc++.h>

using namespace std;

class Example {

public:

int a;

// This function will take

// object as arguments and

// return object
Example add(Example Ea, Example Eb)

Example Ec;

Ec.a = Ec.a + Ea.a + Eb.a;

// returning the object

return Ec;

};

int main()

Example E1, E2, E3;

// Values are initialized

// for both objects

E1.a = 50;

E2.a = 100;

E3.a = 0;

cout << "Initial Values \n";

cout << "Value of object 1: " << E1.a

<< ", \nobject 2: " << E2.a

<< ", \nobject 3: " << E3.a

<< "\n";

// Passing object as an argument

// to function add()

E3 = E3.add(E1, E2);

// Changed values after

// passing object as an argument

cout << "New values \n";

cout << "Value of object 1: " << E1.a


<< ", \nobject 2: " << E2.a

<< ", \nobject 3: " << E3.a

<< "\n";

return 0;

Output:

Initial Values

Value of object 1: 50,

object 2: 100,

object 3: 0

New values

Value of object 1: 50,

object 2: 100,

object 3: 200

Making Outside Function as Inline:


When function is define inside a class is treated as inline function. Normally small function is
defined inside class. We can define member function outside the class definition and still make it
inline. Just by using a qualifier inline in the header line of function definition.
e.g. class SYIT
{
public :
void getdata ( ); // declaration
};

inline void SYIT : : getdata ( )


{ }
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 is not member of any class, 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 SYIT 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 SYIT
{ int roll_no;
char name [20];
public :
void getdata ( );
friend void putdata (SYIT); // function declaration as friend function
} S1;

void SYIT :: getdata ( )


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

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
}

You might also like