0% found this document useful (0 votes)
43 views10 pages

Chap-3 Functions in C++: Function

The document discusses different types of functions in C++ including: 1) The main() function which is the entry point of a C++ program and returns an int value. 2) Function prototypes which specify the name, return type, parameters, and calling convention. 3) Function definitions which implement the body of the function. 4) Inline functions which avoid the overhead of regular function calls by expanding the body inline.

Uploaded by

gobinath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views10 pages

Chap-3 Functions in C++: Function

The document discusses different types of functions in C++ including: 1) The main() function which is the entry point of a C++ program and returns an int value. 2) Function prototypes which specify the name, return type, parameters, and calling convention. 3) Function definitions which implement the body of the function. 4) Inline functions which avoid the overhead of regular function calls by expanding the body inline.

Uploaded by

gobinath
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

BCA-III C++ and Object Oriented Programming

CHAP-3 FUNCTIONS IN C++

FUNCTION

A function is a subprogram that can performed specific task on data and return a value.
Every C++ program has at least one function, main ().

THE MAIN FUNCTION

A C++ program contains a special function called main (). The main () function is an entry point
of program execution; it contains the code that tells the computer what to do as a program
execution.
In C language, it is not specify any return type for the main () function, but in C++ the main ()
returns a value of type int to the operating system, so in C++ main () function is written as:

int main()
{
. . . . .
. . . . .
return 0;
}

But the return type of main () is a default int type, so do not need to write a keyword int in the
main ().

FUNCTION PROTOTYPE

One of the most important features of C++ is the function prototypes.


A function prototype tells the compiler the name of function, the type of data returned by the
function, the number of parameters the function expects to receives, the type of the parameters,
and the order in which these parameters are expected.
SYNTAX
return type function name (argument list) ;
EXAMPLE
int max (int, int, int) ;

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

In above example, the function name is max, return type is int and number of arguments
(parameters) is three and all the parameters types are int.
We can also declare a function with an empty argument list, as in the following example:
void display ( ) ;
In C++, this means that the function does not pass any parameters. It is identical to the statement
void display ( void ) ;

FUNCTION DEFINITION and FUNCTION CALLING

In function declaration, the names of the arguments are dummy variables and therefore they are
optional, but in the function definition names are required because the arguments must be
referenced inside the function.
Example: float volume (int a, float b, float c) ;
{
float v = a * b * c ;
...
...
}
The function volume ( ) can be invoked (called) in a program as follows:
float cube1 = volume (x , y, z) ;
In function calling, the variables are called actual parameters. Remember, the calling statement
should not include type names in the argument list.

CALL BY REFERENCE

In call by value, the called function creates a new variable of the same type as the argument and
copies the argument value in it. So its take more space in memory or duplication of variable
occurs, which will waste the memory space.
But in call by reference, address is passed of original variable instead of value passed to the
function.
The primary advantage of call by reference is that function can access the actual variable in the
calling program.
Below example shows how can we pass the address of original variables using call by reference.

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

Example (Call by Reference)

#include <iostream.h>
#include <conio.h>

void swap (int&, int&) ; //function declaration

void main ()
{
int x , y ;

cout << "Enter the value of x:-" ;


cin >> x ;
cout << "Enter the value of y:-" ;
cin >> y ;

swap (x, y) ; //function call

getch () ;
}

void swap (int&a, int& b) //function definition


{
int temp ;

temp = a ;
a = b ;
b = temp ;
cout << After interchang, the values are:<< a << b;
}
Output:
Enter the value of x:- 10
Enter the value of y:- 20
After interchang, the values are:
20
10

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

RETURN BY REFERENCE

A function can also return a reference, let us consider following function.

#include <iostream.h>
#include <conio.h>

int & max (int&, int&) ; // & indicate reference

void main ()
{
int x , y , z ;

cout << "Enter the value of x:-" ;


cin >> x ;
cout << "Enter the value of y:-" ;
cin >> y ;

z = max (x, y) ; //function call

cout << Maximum is:= << z;


getch () ;
}

int & max (int&a, int& b) //function definition


{
if (a > b)
return a;
else
return b;
}
Output:
Enter the value of x:- 10
Enter the value of y:- 20
Maximum is:= 20

Here return type of max ( ) is int &, the function returns reference to a or b(not value), then
function call such as max(x,y) will a reference to either x or y depending on their values.

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

INLINE FUNCTIONS

One of the advantages of using functions in a program is to save some memory space, when
function is likely to be called many times. However, every time a function is called, it takes a lot
of extra time in executing a series of instructions for tasks such as jumping to the function.
C++ has a solution of this problem. To eliminate the cost of calls to small functions, C++
proposes a new feature called inline function.
An inline function is a function that is extended in line when it is invoked. The compile replaces
the function call with the corresponding function code.
The inline functions are defined as follows:
inline return-type function-name (argument list)
{
Function body ;
}
Example (Inline Function)

#include<iostream.h>
#include <conio.h>

inline int add(int x, int y)


{
int z ;
z = x + y ;
return z;
}
int main ()
{
int a , b ;
cout << "Enter the value of a:-" ;
cin >> a ;
cout << "Enter the value of b:-" ;
cin >> b ;
cout <<Addition is:= << add( a, b) ;
getch () ;
return 0 ;
}
Output:
Enter the value of a:- 10
Enter the value of b:- 5
Addition is:= 15

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

ADVANTAGES OF INLINE FUNCTIONS

Reduces the execution time, an inline function is about 10000+ times faster than a normal function.

Improve program readability.

Strong type checking.

DEFAULT ARGUMENT FUNCTION

Default arguments are specified when the function is declared. The compiler looks at the
prototype to see how many arguments a function uses and alerts the program for possible default
values.
For example,

flaot amount (int a, int b, inc c=10) ;

The value of variable c is default assign to 10 in function declaration, so no need to pass this
value into the function at the time of calling function. For example,
amount( x, y ) ;
In a function calling no need to pass three values, because third value is default argument in
function declaration.
Default parameters are must be assign left to right direction. Below example shows how to
define default argument in function.

Invalid:

int AreaCube (int length=10, int width , int height);

int AreaCube (int length, int width = 25, int height );

int AreaCube (int length=10, int width = 25, int height);

Valid:

int AreaCube (int length, int width , int height = 1);

int AreaCube (int length, int width = 25, int height = 1);

int AreaCube (int length=10, int width=25, int height = 1);

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

Example (Default Argument Function)

#include<iostream.h>
#include<conio.h>
void max(int,int i=20);
main()
{
clrscr();
int a;

cout<<"Enter the value of a: ";


cin>>a;
max(a);
getch();
return 0;
}
void max(int x, int y)
{
if(x>y)
cout<<"Max is= "<<x;
else
cout<<"Max is= "<<y;
}
Output:
Enter the value of a:- 10

Max is:= 20

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

CONSTANT ARGUMENT FUNCTION

In C++, an argument to a function can be declared as const keyword, which are shown below:
int square (const int a) ;
float area (const float r) ;
int strlen (const char * p) ;
int length (const string & s) ;

Example (Constant argument Function)

#include<iostream.h>
#include<conio.h>
void square(const int s=6);
main()
{
clrscr();

square();

getch();
return 0;
}
void square(const int x)
{
int y = x * x ;
cout<<"Square is= "<<y;
}
Output:
Square is:= 36

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

FUNCTION OVERLOADING

Overloading refers to the use of the same thing for different purposes. C++ also permits
overloading of functions. This means that we can use the same function name to create functions
that perform a variety of different tasks. This is known as function polymorphism in OOP.
Using the concept of function overloading, we can design a family of functions with one function
name but with different argument list.
All the functions perform different operations depending on matching of argument list in the
function calls.
For example, a sum ( ) function is overloaded in different types of data, the declaration are:
int sum ( int x, int y ) ;
int sum ( int x, int y, float z ) ;
flaot sum ( float x, float y ) ;
double sum ( double x ) ;

Example (Function Overloading)

#include<iostream.h>
#include<conio.h>
int sum(int,int);
int sum(int,int,int);
float sum(int,float);
double sum(float,float);
main()
{
clrscr();
cout<< sum(10,10)<< endl;
cout<< sum(10,20,30)<< endl;
cout<< sum(10,4.25f)<<endl;
cout<< sum(4.25f,6.25f);
getch();
return 0;
}

Prepared by | Hemang R. Chath


BCA-III C++ and Object Oriented Programming

int sum(int x,int y)


{
return x + y;
}
int sum(int p,int q,int r)
{
return p + q + r;
}
float sum(int t,float u)
{
return t + u;
}
double sum(float i,float j)
{
return i + j;
}
Output:
20
60
14.25
10.5

Prepared by | Hemang R. Chath

You might also like