C++ Functions

In this article you will learn everything about functions in C++, what type of functions are there, how to use them with example.

In programming, functions is a segment of the code that performs a specific task. Depending on whether a function is predefined or created by programmer, there are two types of functions:

  1. User-defined functions
  2. Built-in functions

User-defined Function

A user-defined function consists of the following:

  • Function declaration
  • Function definition

Function declaration or function prototype

Function declaration is a model of a function. It is also known as function prototype. It provides information to compiler about the structure of the function to be used in program. If a user-defined function is defined after main() function compiler will show error because compiler is unaware of user-defined function.

Syntax of function declaration:

Return-type function-name (parameter)

For Example:

Int sqr(int); is a user-defined fumction.

  • Int is a return-type
  • sqr is function-name
  • (int) is parameter

Return-type: it indicates the type of value that will be return by function.

Function-name: it indicates the name of the function.

Parameter: It is the value that is provided to a function when the function is called.

Function definition

A set of statement that explains what a function does is called function definition.

The function definition can be written at the following places:

  • Before main() function
  • After main() function
  • In a separate file
  • Function declaration is not required if function definition is written before main() function.
  • Function declaration is compulsory if function definition is written after main() function.
  • If function definition is written in a separate file then it can be used by including that file in the program using #include preprocessor directive.

The function definition consists of two parts:

  1. Function header
  2. Function body

Function header is similar to function prototype with only one difference is that it is not terminated with semicolon.

Syntax:

Return-type function-name (parameter)

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

Syntax:

{

Statements //body;

}

Function call

To execute the body of function, the user-defined function needs to be activated(called).

For example:

Int sqr(int); Is a function declaration it can call by;

sqr(a);

Example 1:

 

How function works

Here when function starts, system calls main() function that starts executing the body of main() function.

When it reaches to sqr(a); inside the main(), it moves to the int sqr(int a) & starts executing the body of the int sqr(int a).

After this it move back to the main() function from where the body after the call to sqr(a); is executed.

Passing parameter to function

Parameter are the values that provided to a function when the function is called. Parameters are given in the parentheses. If there are many parameters, these are separated by commas.

  • Parameters in function call are called actual parameters.
  • Parameters in function declaration are called formal parameters.