| Previous | Home | Next |
A function is a block of code that performs a specific task. It has a name and it is reusable . it can be executed from as many different parts in a Program as required, it can also return a value to calling program. All executable code resides within a function. It takes in input, does something with it, then give the answer. A C++ program consists of one or more functions. A computer program cannot handle all the tasks by it self. It requests other program like entities called functions in C++. We pass information to the function called arguments which specified when the function is called. A function either can return a value or returns nothing.
Function is a subprogram that helps to reduce coding.
Note:If a program has only one function then it must be the main() function.
return_type function_name(parameter1, parameter2,......)
{
statements
}
- return_type is data type specifier of the data returned by the function.
- function_name is the identifier by which it will be possible to call the function.
- parameters each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration and which acts within the function as a regular local variable. They allow to pass argument to the function when it is called. Different parameters are separated by commas.
- statements is the function's body. It is a block of statements within braces{}.
#include <iostream.h>
using namespace sub;
int subtraction (int a, int b)
{
int r;
r=a-b;
return (r);
}
int main ()
{
int res;
res = subtraction (5,3);
cout << "The result is " << res;
return 0;
}
The result is 2
Function With No Return Type
If we want to show a message on the screen without returning any value. In this case we use void.
#include <iostream.h>
void printmsg()
{
cout<<"This IS my first function";
}
int main()
{
printmsg()
{
printmessage();
return 0;
}
This IS my first function
Default Value In Functions
When we declare a function we can also specify a default value for each parameter. If a value for that parameter is not passed when the function is called, the default value is used.
#include <iostream.h>
void div()
{
int r;
r=a/b;
return(r);
}
int main()
{
cout<<div(20);
cout<<div(20,4);
return 0;
}
10 5
Declaration Of A Function
A function declaration establishes the name of the function and the number and types of its parameters. A function declaration consists of a return type, a name, and a parameter list. We have to declare a function before using it.
#include <iostream.h>
void odd(int a) //function declaration
void even(int a);
int main()
{
int i;
do
{
}
while
{
cout<<"type a number (0 to exit)";
cin>>i;
odd(i);
}
while(i!=o);
return 0;
void odd(int a)
{
if((a%2)!=0)
cout<<"the number is odd";
else even(a);
}
void even(int a)
{
if(a%2)==0)
cout<<"number is even.\n";
else odd(a);
}
Type a number (0 to exit) 7 number is odd Type a number (0 to exit) 6 number is even Type a number (0 to exit) 0
Argument passed by value and reference
The arguments passed to a function can be performed in two ways:
- Pass by value
- Pass by reference
- Pass By Value : when we passed the arguments by value the copies of the values of variables and are passed to the function.
- Pass By Reference :When a variable is passed by reference, it passes the variable to the function definition and not the copies of the value. We use the & for passing the value by reference.
int a=3, b=5; int r; //function declaration r=sum (a, b); int sum (int x, int y) //function call r=sum(3,4)
#include <iostream.h>
void copy(int &a,int &b, int &c)
{
a*=2;
b*=2;
c*2;
}
int main()
{
int x=1,y=3;z=7;
copy(x,y,z);
cout<<"x="<<x", y"<<y<<", z"<<z;
return 0;
}
Recursive Function In C++
When the functions are called by itself, these type of functions are called Recursive Function.
// program of factorial using functuion
#include <iostream>
long fact(long a
{
if(a>1)
return(a*fact(a-1));
else
return(1);
}
int main()
{
long num;
cout<<"please type a number";
cin>>num;
cout<<num<<"!="<<factorial(num);
return 0;
}
Please type a number 5!= 120