Course Content
C++ Tutorial
About Lesson

Functions are a fundamental concept in C++ programming, providing a way to organize code, promote reusability, and enhance readability. In this post, we’ll delve into the basics of defining and calling functions in C++.

Defining Functions

In C++, a function is a block of code that performs a specific task. Defining a function involves specifying its name, return type, parameters, and code block. Let’s break down the components:

// Syntax for defining a function
return_type function_name(parameter_list) {
// Function body
// Code to perform the task
// ...
  • Return Type: Indicates the type of value the function returns. Use void if the function doesn’t return any value.

  • Function Name: A unique identifier for the function. Follows the same naming rules as variables.

  • Parameter List: Input values passed to the function. Enclosed in parentheses and separated by commas.

  • Function Body: The block of code enclosed in curly braces. Contains the statements executed when the function is called.

Example: Simple Addition Function

// Function to add two integers
int add(int a, int b) {
return a + b;

Calling Functions

Once a function is defined, you can call it to execute the code within its body. The syntax for calling a function is straightforward:

// Syntax for calling a function
return_type result = function_name(argument_list);
  • Return Type: If the function returns a value, you can store it in a variable of the specified type.

  • Function Name: The name of the function to be called.

  • Argument List: Actual values passed to the function, matching the order and type of parameters in the function definition.

Example: Using the Addition Function

#include <iostream>

int main() {
// Calling the add function
int sum = add(5, 3);

// Displaying the result
std::cout << "Sum: " << sum << std::endl;

return 0;