WikiGalaxy

Personalize

C++ Functions

Introduction to Functions:

Functions in C++ are blocks of code that perform specific tasks. They help in organizing code, making it reusable and easier to manage.

Syntax of a Function:

A function in C++ is defined with a return type, a name, and a parameter list. The syntax is: returnType functionName(parameterList).


#include <iostream>
using namespace std;

// Function declaration
int add(int a, int b);

int main() {
    int sum = add(5, 3);
    cout << "Sum: " << sum << endl;
    return 0;
}

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

Function Declaration and Definition:

A function must be declared before it is used. The definition provides the actual body of the function.

Return Types:

Functions can return values of different types, such as int, float, char, etc. Use void if no value is returned.

Console Output:

Sum: 8

Function Overloading

Concept of Overloading:

Function overloading allows multiple functions with the same name but different parameters. It helps in handling different data types with the same function logic.


#include <iostream>
using namespace std;

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

double add(double a, double b) {
    return a + b;
}

int main() {
    cout << "Int sum: " << add(5, 3) << endl;
    cout << "Double sum: " << add(5.5, 3.3) << endl;
    return 0;
}
    

Benefits of Overloading:

Overloading improves code readability and usability by allowing the same operation to be performed on different data types.

Console Output:

Int sum: 8

Double sum: 8.8

Default Arguments

Understanding Default Arguments:

Default arguments in C++ functions allow you to provide default values for function parameters. This makes some arguments optional when calling the function.


#include <iostream>
using namespace std;

// Function with default argument
void display(int a, int b = 10) {
    cout << "a: " << a << ", b: " << b << endl;
}

int main() {
    display(5);
    display(5, 15);
    return 0;
}
    

Advantages of Default Arguments:

They simplify function calls by reducing the number of arguments needed, and they enhance flexibility by allowing different levels of function customization.

Console Output:

a: 5, b: 10

a: 5, b: 15

Inline Functions

Concept of Inline Functions:

Inline functions are those whose function body is inserted directly into each call's place, reducing the overhead of function calls.


#include <iostream>
using namespace std;

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

int main() {
    cout << "Square of 5: " << square(5) << endl;
    return 0;
}
    

Benefits of Inline Functions:

They can optimize performance by eliminating the overhead of function calls, especially for small functions. However, excessive use can increase code size.

Console Output:

Square of 5: 25

Recursive Functions

Understanding Recursion:

Recursive functions are those that call themselves to solve a problem. They are useful for problems that can be broken down into smaller, similar problems.


#include <iostream>
using namespace std;

// Recursive function
int factorial(int n) {
    if (n <= 1) return 1;
    else return n * factorial(n - 1);
}

int main() {
    cout << "Factorial of 5: " << factorial(5) << endl;
    return 0;
}
    

Pros and Cons of Recursion:

Recursion can simplify code and make it more readable, but it may lead to performance issues due to excessive function calls and stack usage.

Console Output:

Factorial of 5: 120

Passing by Value vs Reference

Concept of Passing Parameters:

In C++, parameters can be passed by value or by reference. Passing by value copies the argument, while passing by reference allows modifications to the original data.


#include <iostream>
using namespace std;

// Pass by value
void passByValue(int a) {
    a = 10;
}

// Pass by reference
void passByReference(int &a) {
    a = 10;
}

int main() {
    int num = 5;
    passByValue(num);
    cout << "After pass by value: " << num << endl;
    passByReference(num);
    cout << "After pass by reference: " << num << endl;
    return 0;
}
    

Advantages and Disadvantages:

Passing by value is safer as it does not alter the original data, but it can be inefficient for large data. Passing by reference is efficient but can lead to unintended modifications.

Console Output:

After pass by value: 5

After pass by reference: 10

Lambda Expressions

Introduction to Lambdas:

Lambda expressions in C++ allow you to define anonymous functions. They are useful for short snippets of code that are not reused elsewhere.


#include <iostream>
using namespace std;

int main() {
    // Lambda expression
    auto add = [](int a, int b) { return a + b; };
    cout << "Sum using lambda: " << add(5, 3) << endl;
    return 0;
}
    

Benefits of Lambda Expressions:

Lambdas simplify code by eliminating the need for separate function declarations and definitions, making code more concise and readable.

Console Output:

Sum using lambda: 8

Function Templates

Understanding Templates:

Function templates in C++ allow you to create a single function that can work with different data types. They provide flexibility and code reusability.


#include <iostream>
using namespace std;

// Template function
template <typename T>
T add(T a, T b) {
    return a + b;
}

int main() {
    cout << "Int sum: " << add(5, 3) << endl;
    cout << "Double sum: " << add(5.5, 3.3) << endl;
    return 0;
}
    

Advantages of Templates:

Templates reduce code duplication by allowing a single function to handle multiple data types, improving maintainability and efficiency.

Console Output:

Int sum: 8

Double sum: 8.8

Function Pointers

Introduction to Function Pointers:

Function pointers in C++ are used to store the address of a function. They allow functions to be passed as arguments to other functions, enabling dynamic function calls.


#include <iostream>
using namespace std;

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

int main() {
    // Function pointer
    void (*funcPtr)() = greet;
    funcPtr();
    return 0;
}
    

Uses of Function Pointers:

They allow for flexible and dynamic function calls, making them useful in scenarios like callback functions and event-driven programming.

Console Output:

Hello, World!

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025