C++ Function

A function in C++ is a block of code designed to perform a specific task. It is executed when it is called. Functions allow code reuse, improve readability, and make debugging easier. In C++, a function has the following structure:

</>
Copy
return_type function_name(parameter_list) {
    // Function body
    // Code to execute
    return value; // (Optional, only if return_type is not void)
}
  • return_type: Specifies the type of value the function returns.
  • function_name: A unique identifier for the function.
  • parameters_list: (Optional) Input values the function accepts.
  • Function body: The block of code that defines the function’s task.

Simple Example of a Function in C++

This example showcases the basic usage of a function in C++. The greet() function, defined with a void return type, is used to print Hello, World! to the console.

main.cpp

</>
Copy
#include <iostream>
using namespace std;

// Function declaration
void greet() {
    cout << "Hello, World!" << endl;
}

int main() {
    // Function call
    greet();
    return 0;
}

Output

Hello, World!
Program ended with exit code: 0

Explanation

  1. The greet() function is declared and defined to print “Hello, World!” to the console.
  2. In the main() function, we call the greet() function using its name.
  3. The greet() function executes and displays the output.

C++ Function with Parameters

In this example, we will create a function that accepts parameters and performs an operation based on the input.

main.cpp

</>
Copy
#include <iostream>
using namespace std;

// Function with parameters
int add(int a, int b) {
    return a + b;
}

int main() {
    int x = 5, y = 10;
    // Call the function with arguments
    cout << "The sum is: " << add(x, y) << endl;
    return 0;
}

Output

The sum is: 15
Program ended with exit code: 0

Explanation

  1. The add() function takes two parameters, a and b, and returns their sum.
  2. In the main() function, variables x and y are defined and passed to add() as arguments.
  3. In the main() function, the value returned by add(x, y) is displayed using cout.

C++ Function Overloading

Function overloading in C++ is a feature that allows multiple functions to have the same name but with different parameter lists. This enables a single function name to perform different types of tasks based on the number or type of arguments passed to it. The compiler determines which version of the function to invoke by examining the arguments provided during the function call.

In the following example, we will show how multiple functions with the same name multiply can be used for different purposes.

main.cpp

</>
Copy
#include <iostream>
using namespace std;

// Function overloading
int multiply(int a, int b) {
    return a * b;
}

double multiply(double a, double b) {
    return a * b;
}

int main() {
    cout << "Integer multiplication: " << multiply(3, 4) << endl;
    cout << "Double multiplication: " << multiply(2.5, 1.2) << endl;
    return 0;
}

Output

Integer multiplication: 12
Double multiplication: 3
Program ended with exit code: 0

Explanation

  1. Two versions of the multiply() function are defined, one for integers and another for doubles.
  2. The compiler determines which version to call based on the argument types passed to the function.
  3. This demonstrates function overloading, enhancing code readability and flexibility.