Function Calling & Arguments

C++ function calling & argument methods.

* Function calling refers to the process of invoking or executing a function in a program, and function arguments are the values that you pass to a function when you call it.

Calling & Arguments

Functions are blocks of code that perform a specific task. Function calling and passing arguments to functions are fundamental concepts in C++. Let me provide you with a brief overview of how function calling and passing arguments work in C++.

Note: Before calling a function, you need to learn how to declare a function in C++. Learn the Function declaration process from the previous page.

To call a function, you use its name followed by parentheses '()'. If the function takes arguments, you provide them within the parentheses. The values you provide are called arguments.

cpp Copy Code
#include<iostream>

// Function to calculate the sum of two numbers
int sum(int a, int b) {
    return a + b;
}

int main() {
    int num1, num2;
    
    std::cout << "Enter the first number: ";
    std::cin >> num1;

    std::cout << "Enter the second number: ";
    std::cin >> num2;

    // Call the sum function
    int result = sum(num1, num2);

    // Display the result
    std::cout << num1 << " + " << num2 << " = " << result << std::endl;

    return 0;
}

Function calling with arguments: int result = add(5, 3);

Output:
Enter the first number: 10
Enter the second number: 20
10 + 20 = 30

Function arguments can be passed to functions in two main ways: "call by value" and "call by reference." These two mechanisms define how the values of the arguments are passed to the function, and they have different implications for how modifications to the parameters affect the original data.

Call by Value:

In "call by value," the actual values of the arguments are passed to the function. The function receives copies of the values, and any modifications made to the parameters inside the function do not affect the original values outside the function.

// Function to swap values using call by value
void swapByValue(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}
Outcome:
num1 = 5, num2 = 10

* In this example, 'swapByValue' attempts to swap the values of two variables 'a' and 'b'. However, since the values are passed by value, the original 'num1' and 'num2' remain unchanged.

Call by Reference:

In "call by reference," instead of passing the actual values, the memory address (reference) of the arguments is passed to the function. This allows the function to directly modify the original values.

// Function to swap values using call by reference
void swapByReference(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}
Outcome:
num1 = 10, num2 = 5

* In this example, 'swapByReference' swaps the values of two variables 'a' and 'b' using call by reference. As a result, the original values of 'num1' and 'num2' are modified outside the function.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.