Functions in C++ | Declaring, Defining and Calling

Functions in C++ are a group of program statements with a unique name that perform a specific task. Functions are used to provide modularity to a program. By default every C++ program execution starts from the main() function.

Depending on whether a function is predefined or created by user; there are two types of function:

  • Library / Predefined Function
  • User-defined Function

A function declaration tells the compiler about a function’s name, return type, and parameters. A function definition provides the actual body of the function.

functions in c++

Defining Functions in C++
return_type function_name( parameter list ) {
//body of the function
}

A C++ function definition consists of 2 parts:

  • Function header / signature
  • Function body

Following are the components of a C++ function:

  • Return Type: A function may return a value. The return_type is the data type of the value the function returns. Some functions perform operations without returning a value. In this case, the return_type is the keyword void.
  • Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature. To call a function, its name is used.
  • Parameters: A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters.
  • Function Body: The function body contains a collection of statements that define what the function does.
Example of Function Definition
// function returning the max between two numbers
 
int max(int num1, int num2)  {
   // local variable declaration
   int result;
 
   if (num1 > num2)
      result = num1;
   else
      result = num2;
 
   return result; 
}
Declaring a function in C++

Function declaration tell compiler about that there exists a function with the given return type, name and set of parameters. Function declaration does not include the body of the function. The function’s body can be defined separately
A function declaration has the following parts:

return_type function_name( parameter list );

Example of function declaration for the above max function:

int max(int num1, int num2);

Parameter names are not important in function declaration only their type is required, hence the above declaration can also be written as:

int max(int, int);

Function declaration is required when you define a function in one source file and you call that function in another file. In such case, you should declare the function at the top of the file calling the function.

Calling a Function in C++

After declaring and defining a function, to use it we have to perform a function call. Functions can be called by simply using their function name with the necessary arguments passed inside the round brackets.
When a program calls a function, program control is transferred to the called function. A called function performs defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns program control back to the main program.

Example program for function declaration, definition and calling

Run Online

#include <iostream>
using namespace std;
 
// function declaration
int max(int, int);
 
int main () {
   // local variable declaration:
   int num1 = 100;
   int num2 = 200;
   int largest;
 
   // calling a function to get max value.
   largest = max(num1, num2);
 
   cout << "Max value is : " << largest << endl;
 
   return 0;
}
// function returning the max between two numbers
int max(int n1, int n2)  {
   
   if (n1 > n2)
      return n1;
   else
      return n2;
}
Output
Max value is : 200
Default Values for Parameters in Functions

When passing argument values for functions, we can specify default values when no value is passed. This default value will be used when no value is passed.
This is done by using the assignment operator and assigning values for the arguments in the function definition.
Default arguments can be assigned from right to left only. that is if there are 2 arguments to be passed and you want to make one as a default argument, only the right most parameter can be made as a default parameter and not the left one.

Default Argument program example in C++

Run Online

#include <iostream>
using namespace std;
 
int sum(int a, int b=20) {
   return (a+b);
}

int main () {
   // local variable declaration:
   int a = 100;
   int b = 200;
   int result;
   // calling a function to add the values.
   result = sum(a, b);
   cout << "Total value is :" << result << endl;
   // calling a function again as follows.
   result = sum(a);
   cout << "Total value is :" << result << endl;
   return 0;
}
Output
Total value is :300
Total value is :120
Watch it on YouTube

2 thoughts on “Functions in C++ | Declaring, Defining and Calling

  • May 4, 2018 at 9:40 pm
    Permalink

    hi! I am Rohini.s.kulkarni from Hubli . I want you to give me ticks and tricks to grasps stuff fast… specially theory .

    Reply
    • May 6, 2018 at 4:38 pm
      Permalink

      Hello Rohini, In programming related subjects, the only way you can learn things fasters is by practicing code examples. On our website we have program examples with every topic. Try to type the code yourself because thats the best way to go about programming. Develop small programs and applications and then you will get better at this subject. All the best 🙂
      Also checkout this article for 5 ways to learn programming faster – https://simplesnippets.tech/top-5-ways-to-learn-programming-faster/

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *