Function plays very important role in C++ programming language. Every program written in C++ language requires a necessary function called as main function.You can not use this function in your program code nor you can call the main function anywhere in the program. Type of main function is always integer type.
Now you cannot write your whole program code in the main function, because a stage comes when you have to use some statements again and again. A good programmer does not go for copy and paste things like that. Here comes a usage of functions.Functions helps in saving a space. With the help of them , a program can easily readable.
Functions:
"A group of statements that together perform a certain task is a function" or
"A block of statements that together perform to give something productive"
Built-in Functions:
There are numerous built in function in that your program can call like strcat(), memcpy , and also using math.h library , you can use functions like sin(), cos() , sqrt() etc.User defined Functions:
A user defined function is provided by the user or you (programmer). How to make these functions, it is simple as described below.In order to use function you have to know these things.
- Function Declaration
- Function Definition
- Calling a Function
Function Declaration / Prototype:
Function declaration and function prototype is same thing. It tells the compiler about function name, return type and parameters.You have to write this above the main function.Its syntax is as follows
type function_name (parameter1,parameter2 ,,,)
type ==> any valid C++ data type
function_name ==> up to you.
Parameter list ==> when function is called , some values pass to parameters which may be integer, char etc
Note:
- It is always followed by the semicolon. if you not put semicolon, then compiler think it you are going to define function definition.
- parameter type is necessary but variable name is not necessary e.g int x or int
int sum (int x); or int sum (int) ; - order , type and number of parameters is important. it means when function is called , you send something to parameters e.g you send two values one is of integer type and other is of char type so here is you have take care , two parameters you have to define two parameters of integer and char type and order should be same as you maintain order during calling the function.
Function Definition:
This provides the body of function. It can be written either above the main function or below the main function.If you are writing this above the main function , then there is no need of function Declaration and if you are using below the main function , then it is necessary to use function Declaration.Its syntax is follows.
type function_name(parameter 1, parameter 2....)
{
statements;
}
Statements portion is for you write those statements which you think you have to use again and again. So write here and when you want to use just call function. No need to write those statements again and again.
Note:
- function name must be same as in function declaration
- return type must be same as in function definition
- number of parameters, order of parameters and type of parameters must be same
- here you have to write variables name which is optional in function declaration like parameters is of integer type then type function_name ( int x)
This is incorrect in function definition like type function-name (int).
Calling A Function:
Whenever you need to use your function anywhere in the main function you can use your function by calling the function, its syntax is as follow.
function_name(parameter 1, parameter 2, ......);
Note:
- function name must be same as in function declaration and function definition
- take care of order, type and number of parameters you are sending to your function.
- always followed by the semicolon
Example:
Lets do an example on functions.
simple summation program code
This is simply a summation program.I am summing two integer values. So in the function declaration, return type is integer type,sum is the name of function, there are two integer parameters , variable names are x and y followed by the semicolon. In the function definition, return type, function name, parameters are same as in function declaration.when i call the sum function in the main function, i sending two values which are integers values by using a function name which is sum.
Note: i used the same variables x and y in main function as well as in the function , it is not necessary you can use other variables names in the function parameters like int sum (int z , int t);
Note: i used the same variables x and y in main function as well as in the function , it is not necessary you can use other variables names in the function parameters like int sum (int z , int t);
Function with NO parameters and NO return value:
Functions with no parameters and no return value are of limited use. Such function are not returning values but they carry out some basic operation.The type of such function is void which means they are not returning.
void myname(void)
{
cout<<"Nouman";
}
Now see that they are not returning values, because no type of parameter list is void. but they carry out the operation of displaying name.
(a) program code (b) output
Note: function declaration is not there because it is not necessary, when function definition is above the main function.
Call By Value Parameters:
In this method arguments are simply pass to parameter of the function. Changes made to parameters inside the function have no effect on the arguments.
Same example simple summation program.
Same example simple summation program.
Call By Pointer:
In this method you are sending addresses of arguments to pointer parameters of the function. Pointers used the addresses to access the actual arguments used in the call.
(a) program code (b) output
Call By Reference:
Call by reference method of passing arguments copies the reference of an argument into the formal parameter. Inside the function , reference is used to access the actual argument used in the call.It means changes made to parameter affects the passed arguments.
(a) program code (b) output
Default Values for Parameters:
If you are not passing values during calling a function to parameters of the function, then you can specify the value to parameters when you define a function.
(a) program code (b) output