You are on page 1of 6

FUNCTIONS IN C++

Main function:

C does not specify any return type for the main function which is the starting
point for the execution of a program.

The definition of main function is

main()

{
............
// main program statements
............
}

This is perfectly valid because the main () in C does not return any value.

The functions that have a return value should use the return statement for
termination.

The main function in C++ is

int main()

............
............
return 0;
}
FUNCTION PROTOTYPING:

Function prototyping is one of the major improvements added


to C++ functions. The prototype describes the function interface to the compiler
by giving details such as the number and type of arguments and the type of
return value.

Function prototype is a declaration statement in the calling


program and is of the following form:

type function-name (argument-list);

The argument-list contains the types and names of arguments that must be
passed to the function.

Example:

float volume(int x, float y, float z);

We can also declare a function with an empty argument list, as in the following
example.

void display();----->in C language

In C++, this means that the function does not pass any parameters.

It is identical to the statement.

void display(void);------>in C++


CALL BY REFERENCE:

Reference variables in C++ permit us to pass parameters to the


functions by reference. When we pass arguments by reference, the formal
arguments in the called function become aliases to the actual arguments in the
calling function.

This means that when the function is working with its own
arguments, it is actually working on the original data.

Consider the following example:-

# include<iostream.h>
void main()
{
void swap(int &, int &);
int a, b;
cout<<"enter a and b";
cin>>a>>b
cout<<"before swapping"<<a<<b;
swap(a, b);
cout<<"after swapping"<<a<<b;
}
void swap(int &x, int &y)
{
int t;
t=x;
x=y;
y=t;
}
Return by Reference:

A function can also return a reference. Consider the following function:


int & max(int &x, int &y)
{
if (x>y)
return x;
else
return y;
}

Since the return type of max () is int &, the function returns reference to x or
y(and not the values). Then a function call such as max (a,b) will yield a
reference to either a or b depending on their values. This means that this
function call can appear on the left-hand side of an assignment statement.
That is, the statement
Max (a,b) = -1
is legal and assigns -1 to a if it is larger, otherwise -1 to b.
Inline Functions:

By using these inline functions, the execution speed of the program becomes
fast. Whenever the function is defined as inline, and when it is executed, the
code in the inline function will be replaced in the calling function. In this there
will be no moving of control to the function and again returning back the control
to the calling function.

The syntax of representing inline function is shown below.

Inline returntype function-name(parameters)

//body of a main function

Example:

//to find the square of a number using inline function

# include <iostream.h>
Inline float square (float x)
{
x=x*x;
return(x);
}
void main()
{
float num;
cout<<Enter a number<float>:;
cin>>num;
cout<<Its square=<<square(num);
}
DEFAULT ARGUMENTS:

In a C++ function call, when one or more arguments are omitted, the function
may be defined to take default values for omitted arguments by providing the
default values in the function prototype. These arguments are supplied by the
compiler when they are not specified by the programmer explicitly.

Example:
# include <iostream.h>
void show (int x=5, float y=1.1, char *z=JAVA)
{
cout<<Integer:<<x<<endl;
cout<<float:<<y<<endl;
cout<<string:<<z<<endl;
}
void main()
{
show(10, 9.3, C++);
show(20, 7.9);
show(30);
show();
}

OUTPUT:

Integer: 10 20 30 5
Float: 9.3 7.9 1.1 1.1
String: C++ JAVA JAVA JAVA

You might also like