A function is an activity or a code that performs operations. A function is executed when it is called. Functions are the small modules of the program that perform specified operations. They are used when we have to perform certain tasks repeatedly. They are written only once but may be referred at several points in a program.
- Easier to code
- Easier to understand
- Easier to maintain & debug
- They enable the programmer to develop programs for complex problems
C++ language provides the following types of functions:
- User defined function
- Built in functions
User defined functions
These types of functions are described by the user. Users name these functions according to what operations they were built to perform.
Built in functions
These types of functions are already described or defined in C++ language by the manufacturer. C++ programming language has a wide range of built in functions. For example getch() is used to get character or input from the user. To use this function a header file is used that is <conio.h>.
The process of writing a function in C++ language is very simple. A function consists of the following parts:
- Function header
- Function body
Return-type function-name (parameters)
- Function header:
The first line of a function definition is known as function header. The following line shows the basic syntax of writing the function header:
Return-type Function-name (parameters);
Return-type: It stipulates the data type of value that will be returned by function.
Function-name: It is the name of the function.
Parameters: these are the operands (values) on which the operations will be done.
- Function body:
The set of statements which are executed inside a function is known as function body.
Function declaration is also known as function prototype. It provides information to the compiler about the structure of the function to be used in the program. It ends with a semicolon.
A set of statements that explains what a function does is called function definition. The function definition can be written at the following places:
- Before main ()
- After main ()
- In a separate file
Declaring the function is not required if it is defined before the main() function, but it is compulsory when defined after the main() function. If function declaration is written in a separate file then it can be included in the program using #include preprocessor directive.
The statement that activates a function is known as function call. A function is called with its name. Function name is followed by necessary parameters in the parentheses. If there are many parameters, they are split up by a comma. If there is no parameter, empty parentheses are used. When a function is called, the following steps take place:
- The control moves to the function that is called.
- All statements in the function body are executed.
- The control returns back to the calling function.
# include <iostream>
Void main ()
Cout<<”programming makes life interesting”;
Parameters are the values that are provided to a function when it is called.
- Parameters in function call are called actual parameters.
- Parameters in function declaration are called formal parameters.
A function can return a single value. The return type in the function declaration indicates the type of value returned by a function. For example, int is used as return data type if the function returns integer value. If the function returns no value, the keyword void is used as return type. The keyword return is used to return the value back to the calling function.
When the return statement is executed in a function, the controls moves back to the calling function along with the returned value.
The syntax of returning a value is as follows:
Expression: it can be a variable, constant or an arithmetic expression whose value is returned to the calling function.
The calling function can use the returned value in the following ways:
- Assignment statement
- Arithmetic expression
- Output statement
The calling function can store the returned value in a variable and then use this variable in the program.
The calling function can use the returned value directly in an arithmetic expression.
The calling function can use the returned value directly in an output statement.