Types Of Functions
}A function with no parameter and no return value
#include
#include
int main()
{
int a=10,b=20;
void mul(int,int);
mul(a,b); //actual arguments
getch();
}
void mul(int x, int y) //formal arguments
{
int s;
s=x*y;
Cout<<"mul is" << s;
}
}A function with parameter and no return value
#include<iostream>
using namespace std;
int mul(a,b) //funciton declaration
int main ()
{
int a=10,b=25;
mul(a,b); // actual argument & function calling
getch();
return 0;
}
void mul(int a,int b) //formal argument
{
int c;
c=a*b;
cout<<"product of "<<a<<" and "<<b<<" is:"<<c;
}
}A function with parameter and return value
#include#include<conio.h>
int main()
{
int a=10,b=20,c;
int max(int,int);
c=max(a,b);
cout<<“greatest no is” <
getch();
}
int max(int x, int y)
{
if(x>y)
return(x);
else{
return(y);
}
}
1.A function with no parameter and no return value
#include
#include
using namespace std;
int main()
{
void print(); //function declaration
cout<<"no parameter and no return value";
print(); //function calling
getch();
}
void print() //function definition
{
for(int i=1;i<=30;i++)
{
cout<<"*";
}
Cout<<"n";
}
I. There is no data transfer between calling and called function
II. The function is only executed and nothing is obtained
III. Such functions may be used to print some messages, draw
stars etc.
2.A function with parameter and no return value
3.A function with parameter and return value
3.A function with parameter and return value
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
int a=10,b=20,c;
int max(int,int);
c=max(a,b);
cout<<"greatest no is "<<c;
getch();
}
int max(int x,int y)
{
if(x>y){
return x;
}
else {
return y;
}
}
4.A function without parameter and return value
#include<iostream>
#include<conio.h>
using namespace std;
int main ()
{
int a=10,b=20;
int sum();
int c=sum(); //actual arguments
cout<<"sum is "<<c;
getch();
}
int sum() //formal arguments
{
int x=10,b=20;
return (x+y); //return value
}
INLINE FUNCTION
• One of the objectives of using functions is to save some memory space, which becomes appreciable when a function is likely to be called many times.
• Every time a function is called, it takes a lot of extra time in executing a series of instructions.
• To eliminate the cost of calls to small functions, C++ proposes inline function.
• An inline function is a function that expanded in line when it is invoked.
• The compiler replaces the function call with a corresponding function code.
• inline function header
{
function body
}
{
function body
}
• Prefix the keyword inline to the function definition.
#include
inline float mul(float x, float y)
{
return(x*y);
}
inline double div(double p, double q)
{
return(p/q);
}
int main()
{
float a = 12.345;
float b = 9.82;
cout<< mul(a,b) << "n";
cout<
0 Comments