show (); //function call
}
void show ()
{
--------- //function body
---------
---------
}
main ()
{
__________
__________
}
In C, above statement is valid because the main () in C dose not return any value i.e. the function is void type.
In C++, the main () function return a value of type int to the operating system. Therefore explicitly define main () function as the matching of
int main ()]
Function prototype ·Gives the compiler the details about the functions such as the number and the type of return values. ·It is declaration statement
type function_name (argument_list):
E.g. int area (int x, int y);
float volume (int x, int y, int z); ·Function can also declared as empty argument list as in the following example:
Void display ();
It means that the function doesn't pass any parameters. It is similar to
Void display (void);
PASSING VARIABLES IN A FUNCTION:
1.PASSING BY VALUE.
2.PASSING BY REFERENCE.
3.PASSING BY ADRESS.
1. Passing by value
In it, value is passed directly into a function and function creates new variables to hold the values of these variable argument then the actual arguments remained unaltered.
Code:
//pass by value
#include
void swap(int a,int b)
{
int temp=a;
a=b;
b=temp;
cout<<"\na="<
2. Return by reference: Syntax:
int & max(int &x, int &y)
Code:
{
if (x>y)
return x;
else
return y;
}
Return type of max () is int&.
The function return reference to x or y (and not the value)
The function call such as max (a, b) will yield a reference to either a or b depending on their values.
max (a, b) =-1; is legal and assigns -1 to a if it is large otherwise -1 to b.
Q. Write a program that illustrate pass by reference and return by reference.
Code:
#include
#include
int &max(int &a,int &b)
{
if(a>b)
return a;
else
return b;
}
void main()
{
int x,y;
clrscr();
cout<<"enter the first value ";
cin>>x;
cout<<"\nenter the second value ";
cin>>y;
cout<<"\nThe greater is "<
getch();
} /*output
enter the first value (let)5
enter the second value (let)4
The greater is 5
*/
TYPES OF FUNCTION:
PASS AND RETURN
NO PASS BUT RETURN
PASS BUT NO RETURN
NO PASS AND NO RETURN
1.Pass and return
Code:
//value and return
#include
#include
#include
float sum(int x, int n)
{
float sum=0;
for(int i=1;i<=n;i++)
sum+=(float)pow(x,i)/i;
return sum;
}
void main()
{
int x,n;
clrscr();
cout<<"enter value for x ";
cin>>x;
cout<<"how many terms ";
cin>>n;
cout<<"sum="<
getch();
}
/*
output
enter value for x (12)
how many terms (2)
sum=84
*/
2. No pass but return
Code:
/no value with return
#include
#include
#include
float sum()
{
int x,n;
float sum=0;
clrscr();
cout<<"enter value for x";
cin>>x;
cout<<"how many terms";
cin>>n;
for(int i=1;i<=n;i++)
sum+=(float)pow(x,i)/i;
cout<
return sum;
}
void main()
{
int x,n;
clrscr();
cout<<"enter value for x";
cin>>x;
cout<<"how many terms";
cin>>n;
sum(x,n);
getch();
}
4. No pass and no return
Code:
//no value no return
#include
#include
#include
void sum()
{
int x,n;
float sum=0;
clrscr();
cout<<"enter value for x";
cin>>x;
cout<<"how many terms";
cin>>n;
for(int i=1;i<=n;i++)
sum+=(float)pow(x,i)/i;
cout<
getch();
}
void main()
{
sum();
}
SCOPE RESOLUTION OPERATOR ( ::)
C++ supports a mechanism to excess a global variable from a function in which a local variable is defined with the same name as a global variable. It is achieved using scope resolution operator. Syntax: :: Global variable
It directs the compiler to excess the global variable instead of local variable with the same variable name.
Code:
//use of scope resolution operator
#include
#include
int x=5; //global variable
void main()
{
int x=10; //local variable
clrscr();
cout<<"local varable is "<
cout<<"\nglobal variable is "<<::x;
getch();
}
/*
output
local variable is 10
global variable is 5
*/
REFERENCE VARIABLE:
A reference variable provides alias that is alternative name of the variable i.e. previously defined.
Code:
// example of reference variable
#include
#include
void main()
{
int x=5;
clrscr();
int &y=x; //y is alias of x
cout<<"x="<
y++;
cout<<"\nx="<
getch();
}
/* output
x=5
x=6
*/
INLINE FUNCTION:
While inline function is called the inline code of the function is inserted at the place of the function call and compiled with other source code together. By using inline function execution time is reduced because there is no transfer and return back to control. But if function have long code inline function isn't suitable. ·It saves memory. ·The function needn't be duplicated in memory ·It is analogous to macros in C. the major drawback with macros is that they are not really function and therefore the usual error checking doesn't occur during compilation.
Some of situations where inline expansion may not work are:
1.For function returning values if a loop, a switch or goto exists.
2.For functions not returning values, if a return statement exists.
3.If function contain static variables.
4.If inline functions are recursive.
DEFAULT ARGUMENTS: ·C++ allows us to call a function without specifying all its arguments. In such case the function assigns a default value to the program which doesn't have a matching argument in the function call. ·Default values are specified when the function is declared. ·We must add the default from right to left.
FUNCTION OVERLOADING:
Function overloading refers to the use of same function name for different task. While an overloaded function is called the function with matching argument and return type is invoked.
int max(int, int);
int max(int, int, int);
float max(float, float);
A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique.
The function selection involves the following steps:
1.The complier first tries to find an exact match in which the type of actual arguments are the same and used that function.
2.If an exact match is not found, the complier uses integral promotion to the actual arguments such as-char to int float to double to find match.
3.When either of them fails, the complier tries to use built in conversion (the impulsive assignment conversion) to the actual arguments and then uses the function where match is unique. If the conversion is possible to have multiple messages then the compiler will generate the error message. E.g.
long square(long n);
double square(double x);
A function call such as square(10) will cause an error because int arguments can be converted to either long are double.
4. If all the slopes fail then the complier will try the s=user defined conversion in combination with integral promotion and built in conversion to find the unique match(). User defined conversion are often used in handling class and object.
HTML Code:
//example of function overloading
#include<iostream.h>
#include<conio.h>
int volume(int s)
{
return(s*s*s);
}
double volume(double r, int h)
{
return(3.14159*r*r*h);
}
long volume(long l,int b, int h)
{
return(l*b*h);
}
void main()
{
clrscr();
cout<
cout<
cout<
getch();
}