C++ Function
A function is a set of statements that executes a task. In C++, functions are categorized into two different categories on behalf of argument (parameter) pass.
- Function with parameter (return_type function_name(paramenter);)
- Function without parameter (return_type function_name();)
Function which takes parameter in function is called function with parameter. A function which does not take parameter in function is called function without parameter.
Function with a parameter has further categorized into two different categories.
- Call by value
- Call by reference.
C++ call by value
In
call by value function, action performed is done over the copy of actual value pass in the parameter. So that in
call by value the actual value of the program
never affected. The action on value is performed on the current function.
C++ call by value Example
#include<iostream>
using namespace std;
void swap(int,int);
int main()
{
int a=5,b=10;
cout<<"Before swapping\nValue of a is :"<<a<<"\nValue of b is :"<<b;
swap(a,b);
cout<<"\nOutside function after swapping\nValue of a is :"
<<a<<"\nValue of b is "<<b;
return 0;
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"\nValue inside function after swapping\nValue of a is
:"
<<a<<"\nValue of b is :"<<b;
}
Output:
Before swapping
Value of a is : 5
Value of b is: 10
Value inside function after swapping
Value of a is :10
Value of b is :5
Outside function after swapping
Value of a is : 5
Value of b is: 10