1. Function 2.Call by value 3.Call By Reference 4. Inline function 5. Function With Default Arguments 6. Friend function
Function:
The function is collection or group of instruction that performs a certain task.
Every program of C has at least one function and that function is main ( ).And main () is the main part of the program. Without main( ) we can't run the program in c.
The function contains return type, parameter, and circle bracket ( ). ( ) it is one kind of identity of function every function has it.
For more about function, i already explain on this page Function in C
Call by value :
In call by value, the value of the variable is passed during the function call.
And copy this value in another variable at function definition.
In call by value, the original value in calling function will never change after execution of the function.
Example:
#include<iostream.h>
#inlude<conio.h>
void swap(int a,int b){
int temp=a;
a=b;
b=temp;
}
void main()
{
int x,y;
cout<<"Enter the value of a and b";
cin>>x>>y;
swap(x,y);
cout<<"\n after swapping a="<<a<<"b="<<b;
getch();
}
Output:
Enter the value of a and b
4
7
after swapping a=7 b=4
Call By Reference :
In call by reference, reference is passed during the function call.
The formal arguments in the called function become aliases to the actual function call.
In call by reference, the original value in calling function will change after execution of the function.
Example:
#include<iostream.h>
#inlude<conio.h>
void swap(int &a,int &b) //it is only difference from above program
{
int temp=a;
a=b;
b=temp;
}
void main()
{
int x,y;
cout<<"Enter the value of a and b";
cin>>x>>y;
swap(x,y);
cout<<"\n after swapping a="<<a<<"b="<<b;
getch();
}
Output:
Enter the value of a and b
4
7
after swapping a=7 b=4
What is the inline function?
The functions can be made inline by adding prefix inline to the function definition.
An inline function is a function that is expanded in line when it is invoked.
The compiler replaces the function call with the corresponding function code.
Inline function saves the time of calling function, saving registers, pushing arguments onto the stack and returning from function.
Preprocessor macros are popular in C, which has similar kind of advantages mentioned above.
The major drawback of macros is that they do not really function.
Therefore, the usual error checking does not occur during execution of macros.
We should be careful while using the inline function. If the function has very few lines of code and simple expressions then only it should be used.
Critical situations for the inline function:
1) If a loop, a switch or a goto exists in the function body.
2) If function is not returning any value.
3) If function contains static variables.
4) If function is recursive.
Example:
inline int cude(int n)
{
return n*n*n;
}
void main()
{
int c;
c=cube(10);
cout<<c;
}
Explain Function With Default Arguments
• C++ allows us to call a function without specifying all its arguments.
• In such cases, the function assigns a default value to the parameter.
• Default values are specified when the function is declared.
• We must add default arguments from right to left.
• We cannot provide a default value to a particular argument in the middle of an argument list.
• Default arguments are useful in situations where some arguments always have the same value.
For Example, passing marks.
• Legal and illegal default arguments
void f(int a,int b,int c=0) //legal
void f(int a,int b=0,int c=0) //legal
void f(int a=0,int b,int c=0) //illlegal
void f(int a=0,int b,int c) //illegal
void f(int a=0,int b=0,int c=0) //legal
Example:
#include<iostream.h>
#include<conio.h>
void f(int a=0,int b=0)
{
cout<<"a="<<a<<"b="<<b;
}
void main()
{
f(7);
f(7,8);
getch();
}
Output:
a=7 b=0
a=7 b=8
What is friend function?
A friend function is a function which is declared using friend keyword.
It is not a member Of the class but it has access to the private and protected members Of the class.
It is not in the scope Of the class to which it has been declared as a friend.
It cannot access the member names directly.
It can be declared either in public or private part Of the class.
It is not a member Of the class so it cannot be called using the object.
Usually, it has the objects as arguments.
It is the normal external function which is given special access privileges.
Syntax:
class ABC {
public :
friend void xyz (void) ; // declaration
};
Example:
#include<iostream.h>
#include<conio.h>
class numbers {
int numl, num2;
public:
void setdata(int a, int b) ;
friend int add (numbers N) ;
};
void numbers : :setdata(int a, int b)
{
numl=a ;
num2=b;
}
int add (numbers N)
{
return (N. num2+N. num2) ;
}
main ( ) {
numbers NI;
NI.setdata (10, 20)
cout<<"sum="<<add(NI);
getch();
}
Output:
sum=30
add is a friend function Of the class numbers so it can access all members Of the class(private, public and protected).