Function and its Concepts in C++


There are two types of function in C++ Language:

  1. Standard Built-in Library Functions: Predefined in C++
  2. User-defined Function: Created by users

Built-in functions

The C++ standard library provides numerous built-in functions that your program can call. We do not need to declare or define these functions as they are already written in the C++ libraries. Example: iostream, strcat(), memecpy(). We can directly call them as required.

User-defined Function

A function is a block of statements/code that runs together only when it is called to perform a specific task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions. You can divide up your code into separate functions, and they are important for reusing code that is Define the code once and use it many times. When the function is called from any part of the program, it executes the block of codes defined in the body of the function. The function has to be called. You can also pass data, known as parameters, into a function.

C++ allows the programmer to define their own function.

Function Declaration

A function declaration informs the compiler about a function’s name, return type, and parameters. To declare a function, specify the name of the function, followed by parentheses ():

return_type Function_name() 
  // body
  • return_type is the type of data the function will return
  • Function_name() is the name of the function (definition)
  • the body has the code that defines what the function should do (declaration)

Function Definition

A function definition is the body of the function. While creating a C++ function, we give a definition of what the function has to do.

A C++ function definition consists of a function header and a function body. Various parts of a function are:

  • Return Type − A function may or may not have a return value. The return_type is the datatype of the value that the function returns. In the case there is no return value, the return_type is the keyword void.
  • Function Name − This is the name of the function. The function name and the parameter list together are known as the function signature.
  • Parameters − A parameter is like a placeholder. When a function is called, a value is passed to the parameter. The parameter list refers to the number, order, and type of the parameters of a function. (Parameters are optional).
  • Function Body − Function body holds a group of statements that define what the function does.

Call a Function

Declared functions are not executed at the moment. They are “saved for later use”, and will only be executed when they are called.

Syntax: Write the function’s name followed by two parentheses () and a semicolon ;

variable = function_name ( args, ...); 

Function Parameters

Parameters and Arguments

Data can be sent to functions as a parameter. Parameters are a type of variables inside the function.

Parameters are given after the function name, inside the round brackets. You can add as many parameters separated with commas.

void function_name(parameter1, parameter2, parameter3) 
  // body

Default Parameter
You can use a default parameter value, by using the equals sign that is the assignment operator (=). When you define a function by assigning values for the arguments in the function definition, you can specify a default value for each of the last parameters. This value will be used if the corresponding argument is blank when the function is being called.

Multiple Parameter
You can add as many parameters as you want inside a function.

Benefits of Using User-Defined Functions

  • Make the code reusable, that is we can declare them once in the program and use them multiple times.
  • Divide the large program into smaller blocks of code.
  • Increase readability in the program.

Function Overloading

By function overloading, two or more functions have the same name with different parameters. Function Overloading example in C++ language:

#include <iostream>
using namespace std;
// Same name different parameters

void print(int i) 
  cout << " Print int: " << i << endl;
void print(char const *c) 
  cout << " Print char: " << c << endl;

int main() 
  return 0;
Print int: 10 
Print char: ten

Advantages of Function Overloading:

  1. The function can perform different operations.
  2. Eliminates the use of different function names for the same kind of operations.
  3. Increases Readability of program.
  4. Increases maintainability of the code.
  5. Brings flexibility to the program.
We will be happy to hear your thoughts

Leave a reply

Open chat
Scan the code
Can we help you?