Functions whose definitions are
defined by the user(s) are known as user defined functions. In the
practical world of solving an application problem which needs to be programmed,
it is very hard to sit at one time and to write down the solution to the whole
problem completely. Instead, it is better to think how we can fragment the
whole complete problem into smaller sub parts and then how to solve those sub
parts individually one after another then we can think on, how to unite all of
those individual objects together into to form a single unit in-order to
accomplish the goal.
This
fragmenting of a big problem into smaller sub-problems is known as
modularization. Functions help us in modularization of the program to reduce
the complexity of the program development. Also it helps us to trace for the
errors in our program.
So we can say – Functions are the main
building blocks of programs. They are actually the named group of program
statements, designated to a particular task.
Example:
//function
sum() to print the sum of two numbers
void
sum(void)
{
int a,b;
cout<<"\n Enter two numbers:
";
cin>>a>>b;
cout<<a+b;
}
Now let’s see the terms associated
with a function.
- Function Prototype
- Function Definition
- Function Calling
Example
So, form the above explanation it
is very clear that –
- Function prototype contains return type of the function, which can be void, int, float etc., function name and the list of data types of the formal arguments or variables with their data types to be used.
- Function definition contains return type of the function, function name, list of formal arguments or variables with their associated data types and the body of the function definition. The body of function definition is denoted by a pair of curly braces ({ }). The function body contains a list of program statements that defines the designated task of the function and a ‘return’ statement if the return type of function is not ‘void’. However you can write simply return; (return statement without any returning value) even if the return type of the function is ‘void’
Example:
void sum(int a, int b)
{
cout<<"Sum = "<<a+b;
return;
}
- Calling of a function contains no return types it only contains name of the function to be called and the actual parameters (constants or the variables).
nice dude
ReplyDeleteVery Nice Explanation...... Good!
ReplyDeleteOutstanding Explaination.....Very Good!
ReplyDeleteThanks! For your positive comment. Just trying a little to do good things.
ReplyDelete