Functions in C++ (original) (raw)

Last Updated : 25 Apr, 2026

A function is a reusable block of code that performs a specific task. It divides a program into smaller logical units, improves readability, and makes code easier to maintain. A function can accept parameters, execute statements, and optionally return a value.

**Note: C++ also supports advanced features like function overloading, default arguments, and inline functions, which give more flexibility compared to C.

C++ `

#include using namespace std;

// function definition int square(int x) { return x * x; }

int main() {

// Calling the function
int result = square(5);

cout << "Square of 5 is: " << result << endl;

return 0;

}

`

why_use_functions_

Function Usage

Function Syntax

To work with functions in C++, it is important to understand how they are written, declared, and called. This section covers function syntax, declaration vs definition, and how to call a function in a program.

Function Syntax in C++

A function in C++ follows this general format:

keyword

Basic structure of a C++ function.

Each part has a specific role:

Function Declaration vs Definition

A function declaration introduces a function to the compiler by specifying its return type, name, and parameters without the body, and is used when the function is defined later or in another file.

// Declaration
int add(int, int);

Function definition contains the actual code that specifies what the function does when it is called.

//Definition
int add(int a, int b) {
return a + b;
}

Purpose of Function Declaration in C++

Calling a Function

A function is used by calling its name followed by parentheses, passing required arguments if any, which executes the code inside the function.

C++ `

#include using namespace std;

void greet() { cout << "Welcome to C++ Programming!" << endl; } int multiply(int a, int b) { return a * b; }

int main() {

greet();
int result = multiply(4, 5);
cout << "Multiplication result: " << result << endl;
return 0;

}

`

Output

Welcome to C++ Programming! Multiplication result: 20

**Explanation:

**Note: Function calls allow you to reuse code easily. You can call the same function multiple times with different inputs to perform repeated tasks in a structured and clean way.

Parameters or Arguments

A function can accept input values called arguments, which are passed during the function call and received through typed parameters defined inside the function’s parentheses.

**Syntax:

return_type name(type1 name1, type2 name2...) {
// Function body
return val;
}

C++ `

#include using namespace std;

void printNum(int n){ cout << n << endl; }

int main() { int num1 = 10; int num2 = 99;

printNum(num1);
printNum(num2);
return 0;

}

`

**Explanation:

**Note: A function can only take as many arguments as specified in the function definition and it is compulsory to pass them while calling it. Also, they should be of same type as in the function definition.

Types of Functions in C++

In C++, functions can be broadly categorized based on two criteria:

**1. Based on origin

**2. Based on input and return type

User-defined functions can be further classified based on whether they accept parameters or return a value:

**Note: These types help you design functions according to the task they need to perform. Choosing the right form improves code flexibility and clarity.

Related Article: