Introduction
A function is a block of code that has a name
and it has a property that it is reusable. It can be executed from as many
different points in a C++ Program as required. Function groups a number of
program statements into a unit and gives it a name. This unit can be invoked
from other parts of a program. A computer program cannot handle all the tasks by
it self. Instead its requests other program like entities called functions in
C++ to get its tasks done. A function is a self contained block of statements
that perform a coherent task of same kind The name of the function is unique in
a C++ Program and is Global. It names that a function can be accessed from any
location with in a C++ Program. We pass information to the function called
arguments specified when the function is called. And the function either returns
some value to the point it was called from or returns nothing.
Function in a C program has some properties
- Every function has a unique name. This
name is used to call function from main() function. A function can be called
from within another function.
- A function performs a specific task. A
task is a distinct job that your program must perform as a part of its
overall operation such as adding two or more integer, sorting an array into
numerical order, or calculating a cube root.
- A function returns a value to the calling
program. This is optional and depends upon the task your function is going
to accomplish. Suppose you want to just show few lines through function then
it is not necessary to return a value.
Example : Through a function print a
value.
#include
<iostream>
using
namespace
std;|
int
addition (int
a,
int
b)
{
int
r;
r=a+b;
return
(r);
}
int
main ()
{
int
z;
z = addition (5,3);
cout <<
"The
result is = "
<< z;
return
0;
}
Output
The return is = 8