WikiGalaxy

Personalize

C++ Function Parameters

Introduction to Function Parameters:

Function parameters in C++ allow you to pass data into functions, enabling code reusability and modularity. Parameters can be passed by value, by reference, or by pointer, each serving different purposes and use cases.


#include <iostream>
void printNumber(int num) {
    std::cout << "Number: " << num << std::endl;
}
int main() {
    printNumber(5);
    return 0;
}
    

Passing by Value:

In this method, a copy of the actual parameter is passed to the function. Changes made to the parameter inside the function do not affect the original argument.

Console Output:

Number: 5

Passing by Reference

Understanding References:

When passing by reference, the function receives a reference to the actual parameter, allowing modifications to the original data.


#include <iostream>
void increment(int &num) {
    num++;
}
int main() {
    int value = 10;
    increment(value);
    std::cout << "Value: " << value << std::endl;
    return 0;
}
    

Effect on Original Data:

The original variable is modified, as the function directly accesses the memory location of the argument.

Console Output:

Value: 11

Passing by Pointer

Pointer Parameters:

Pointers allow functions to modify the actual parameter by passing the address of the variable. This method is useful for dynamic memory allocation.


#include <iostream>
void setToZero(int *num) {
    *num = 0;
}
int main() {
    int value = 20;
    setToZero(&value);
    std::cout << "Value: " << value << std::endl;
    return 0;
}
    

Direct Memory Access:

The function can directly modify the value at the memory location pointed by the pointer.

Console Output:

Value: 0

Default Parameters

Using Default Arguments:

C++ allows you to provide default values for function parameters. If no argument is passed, the default value is used.


#include <iostream>
void greet(std::string name = "Guest") {
    std::cout << "Hello, " << name << "!" << std::endl;
}
int main() {
    greet("Alice");
    greet();
    return 0;
}
    

Flexibility in Function Calls:

This feature enhances flexibility, allowing function calls with fewer arguments.

Console Output:

Hello, Alice!

Hello, Guest!

Function Overloading

Overloading Functions:

C++ supports function overloading, allowing multiple functions with the same name but different parameter lists.


#include <iostream>
void print(int num) {
    std::cout << "Integer: " << num << std::endl;
}
void print(double num) {
    std::cout << "Double: " << num << std::endl;
}
int main() {
    print(5);
    print(3.14);
    return 0;
}
    

Enhanced Functionality:

Function overloading provides the ability to handle different data types with a unified interface.

Console Output:

Integer: 5

Double: 3.14

Const Parameters

Immutable Parameters:

Using the 'const' keyword, you can prevent functions from modifying the parameter, ensuring data integrity.


#include <iostream>
void display(const int &num) {
    std::cout << "Number: " << num << std::endl;
}
int main() {
    int val = 7;
    display(val);
    return 0;
}
    

Data Protection:

Const parameters ensure that the function cannot alter the input data, which is crucial for maintaining consistency.

Console Output:

Number: 7

Variadic Functions

Handling Multiple Arguments:

Variadic functions can accept a variable number of arguments, using ellipses (...) to denote the additional parameters.


#include <iostream>
#include <cstdarg>
void printNumbers(int count, ...) {
    va_list args;
    va_start(args, count);
    for (int i = 0; i < count; ++i) {
        int num = va_arg(args, int);
        std::cout << num << " ";
    }
    va_end(args);
    std::cout << std::endl;
}
int main() {
    printNumbers(3, 1, 2, 3);
    return 0;
}
    

Dynamic Argument Handling:

This method is useful for functions that need to handle lists of arguments of varying lengths.

Console Output:

1 2 3

Inline Functions

Optimizing Function Calls:

Inline functions are expanded in place, reducing the overhead of a function call. They are defined using the 'inline' keyword.


#include <iostream>
inline int add(int a, int b) {
    return a + b;
}
int main() {
    std::cout << "Sum: " << add(3, 4) << std::endl;
    return 0;
}
    

Performance Improvement:

Inline functions can improve performance by eliminating the call overhead, especially for small, frequently called functions.

Console Output:

Sum: 7

Lambda Expressions

Anonymous Functions:

Lambdas provide a concise way to define anonymous functions, often used for short-term operations that do not require a separate function.


#include <iostream>
int main() {
    auto add = [](int a, int b) { return a + b; };
    std::cout << "Lambda Sum: " << add(5, 6) << std::endl;
    return 0;
}
    

Convenience and Readability:

Lambdas enhance code readability and are particularly useful in algorithms and event handling.

Console Output:

Lambda Sum: 11

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025