Functions:
Function syntax:
#include<iostream>
using namespace std;
void add();
int main()
{
add();
return 0;
}
void add()
{
int a,b,c;
cout<<”Enter a” <<endl;
cin>>a;
cout<<”Enter b”<<endl;
cin>>b;
c=a+b;
cout<<”c value “<<c <<endl;
}
1. Function declaration or prototype:
Function declaration tells compiler that same function with respective name and argument are somewhere in program. If declaration is not done at the beginning then the function must be defined before main () function.
2. Function call:
Function is call inside from the main () function.
3. Function definition:
All the statement which shows certain operation, is placed inside the function and is called, function is defined.
Syntax for function:
Data_type + function_name (arguments)
Advantages:
1. Save memory space.
2. Make program clear.
3. Easy to debug.
Function Overloading:
Function overloading occurs in the program when no of functions have same name but different arguments.
Advantages:
Having same name for all function reduces ambiguity because it is easy to remember the name of function.
Example:
#include<iostream>
using namespace std;
void m1(int i)
{
cout<<"Int-args:"<<endl;
}
void m1(float f)
{
cout<<"Float-args:"<<endl;
}
int main()
{
m1(2);
m1(2.3f);
return 0;
}
Output:
Int-args:
Float-args:
Here, both functions have same name but different arguments. When integer is passed then “Int-
args:” is executed and when float is passed then “Float-args” is executed.
Promotion Rules
Byte->short
int->long->float ->double-> double
Char
Example:
#include<iostream>
using namespace std;
void m1(char c)
{
cout<<"char-args:"<<endl;
}
void m1(int i)
{
cout<<"int-args:"<<endl;
}
void m1(int i,float f)
{
cout<<"int-float-args:"<<endl;
}
void m1(float f,int i)
{
cout<<"float-int-args:"<<endl;
}
int main()
{
m1('c');
m1(2);
m1(3,2.0);
m1(2.0,3);
return 0;
}
Output:
char-args: int-args: int-float-args: float-int-args
While resolving function overloading if exact matched function is not available then we won’t get any compile time error immediately. First it will promote argument to the next level and checked whether matched function is available or not. If matched function is available then it will be considered.
If matched method is not available then compiler promotes argument one again to the next level. This process will be continued until all possible promotions. Still if the matched function is not available then we will get compile time error.
So here in example, exact matched for char argument is not found so it promotes to next level int and hence finally it matched it and result is shown as “Int-args” for char argument also.
Conversion rules:
#include<iostream> using namespace std;int main(){int a=2,b=3; float c;c=a/b;cout<<"The value of c:"<<c<<endl;
return 0;
}
Output :
The value of c: 0
Here, the output must be 0.666667 but it shows 0. Why?
Ans: (2/3)=(0.66667) but as default it takes only integer value and so 0 becomes output.
#include<iostream>
using namespace std;
int main(){
int a=2,b=3;
float c;
c=(foat)a/b;
cout<<"The value of c:"<<c<<endl;
return 0;
}
Output
The value of c: 0.666667
Here, float is used as conversion where it takes the float value instead of int
value.
Inline function:
Inline keyword used in function is just a request to compiler to copy the function definition in a place, from where it is called rather than to transfer the control from one memory location to another. It is feasible only when the function definition has small segment of code.
Advantages:
It increases execution time of function.
Example:
#include<iostream>
using namespace std;
inline void add()
{
int a,b,c;
cout<<”Enter a” <<endl;
cin>>a;
cout<<”Enter b”<<endl;
cin>>b;
c=a+b;
cout<<”c value “<<c <<endl;
}
int main()
{
add();
cout <<”again call”<<endl;
add();
return 0;
}