Functions:

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.

 

Benefits of using functions

  1. Easier to code
  2. Easier to understand
  3. Easier to maintain & debug
  4. Reusability
  5. They enable the programmer to develop programs for complex problems

 

Types of functions

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>.

 

Process of writing functions

The process of writing a function in C++ language is very simple. A function consists of the following parts:

  • Function header
  • Function body

SYNTAX

Return-type function-name (parameters)

  1. 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.

  1. Function body:

The set of statements which are executed inside a function is known as function body.

 

Function declaration

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.

Function definition

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.

 

Function Call

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:

  1. The control moves to the function that is called.
  2. All statements in the function body are executed.
  3. The control returns back to the calling function.

EXAMPLE

# include <iostream>

Void main ()

{

Clrscr();

Show ();

}

Void show()

{

Cout<<”programming makes life interesting”;

}

 

Passing parameters to functions

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.

Example

 

passing-parameters-to-functions

 

How a function returns a value

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.

SYNTAX

The syntax of returning a value is as follows:

Return expression;

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:

  1. Assignment statement
  2. Arithmetic expression
  3. Output statement

 

Assignment statement

The calling function can store the returned value in a variable and then use this variable in the program.

assignment-statement

 

Arithmetic expression

The calling function can use the returned value directly in an arithmetic expression.

arithmetic-expression

 

Output Statement

The calling function can use the returned value directly in an output statement.

output-statement