C++ Function Examples

Welcome to The Coding College! Functions are the building blocks of modular and reusable code in C++. In this tutorial, we’ll look at a variety of practical function examples to help you master their usage.

1. Simple Function

Example: Adding Two Numbers

#include <iostream>
using namespace std;

// Function to add two numbers
int add(int a, int b) {
    return a + b;
}

int main() {
    int num1 = 10, num2 = 20;

    cout << "Sum: " << add(num1, num2) << endl;

    return 0;
}

Output:

Sum: 30  

2. Void Function

Example: Displaying a Message

#include <iostream>
using namespace std;

// Function to print a message
void printMessage() {
    cout << "Hello from The Coding College!" << endl;
}

int main() {
    printMessage(); // Call the function
    return 0;
}

Output:

Hello from The Coding College!  

3. Function with Default Parameters

Example: Calculating the Area of a Rectangle

#include <iostream>
using namespace std;

// Function to calculate area with default height
int calculateArea(int length, int height = 5) {
    return length * height;
}

int main() {
    cout << "Area (with default height): " << calculateArea(10) << endl;
    cout << "Area (with custom height): " << calculateArea(10, 15) << endl;

    return 0;
}

Output:

Area (with default height): 50  
Area (with custom height): 150  

4. Function Overloading

Example: Multiplying Integers and Doubles

#include <iostream>
using namespace std;

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

// Function to multiply doubles
double multiply(double a, double b) {
    return a * b;
}

int main() {
    cout << "Multiplying integers: " << multiply(5, 10) << endl;
    cout << "Multiplying doubles: " << multiply(2.5, 4.5) << endl;

    return 0;
}

Output:

Multiplying integers: 50  
Multiplying doubles: 11.25  

5. Recursive Function

Example: Calculating Factorial

#include <iostream>
using namespace std;

// Recursive function to calculate factorial
int factorial(int n) {
    if (n <= 1) return 1; // Base case
    return n * factorial(n - 1); // Recursive case
}

int main() {
    int number = 5;
    cout << "Factorial of " << number << ": " << factorial(number) << endl;

    return 0;
}

Output:

Factorial of 5: 120  

6. Pass by Reference

Example: Swapping Two Numbers

#include <iostream>
using namespace std;

// Function to swap two numbers
void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}

int main() {
    int x = 10, y = 20;

    cout << "Before swapping: x = " << x << ", y = " << y << endl;

    swap(x, y); // Pass by reference

    cout << "After swapping: x = " << x << ", y = " << y << endl;

    return 0;
}

Output:

Before swapping: x = 10, y = 20  
After swapping: x = 20, y = 10  

7. Return Multiple Values Using struct

Example: Returning Sum and Product

#include <iostream>
using namespace std;

struct Result {
    int sum;
    int product;
};

// Function to calculate sum and product
Result calculate(int a, int b) {
    return {a + b, a * b};
}

int main() {
    int x = 5, y = 10;
    Result res = calculate(x, y);

    cout << "Sum: " << res.sum << ", Product: " << res.product << endl;

    return 0;
}

Output:

Sum: 15, Product: 50  

8. Lambda Functions

Example: Inline Functions for Quick Calculations

#include <iostream>
using namespace std;

int main() {
    auto add = [](int a, int b) { return a + b; };
    auto square = [](int x) { return x * x; };

    cout << "Sum: " << add(10, 20) << endl;
    cout << "Square: " << square(5) << endl;

    return 0;
}

Output:

Sum: 30  
Square: 25  

9. Using Pointers in Functions

Example: Find Maximum Number

#include <iostream>
using namespace std;

// Function to find the maximum number
void findMax(int *a, int *b, int *max) {
    *max = (*a > *b) ? *a : *b;
}

int main() {
    int x = 15, y = 20, max;
    findMax(&x, &y, &max);

    cout << "Maximum: " << max << endl;

    return 0;
}

Output:

Maximum: 20  

10. Using Arrays in Functions

Example: Calculate Sum of an Array

#include <iostream>
using namespace std;

int calculateSum(int arr[], int size) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += arr[i];
    }
    return sum;
}

int main() {
    int numbers[] = {1, 2, 3, 4, 5};
    int size = sizeof(numbers) / sizeof(numbers[0]);

    cout << "Sum of array: " << calculateSum(numbers, size) << endl;

    return 0;
}

Output:

Sum of array: 15  

Explore More on The Coding College

Functions are essential for structuring your code and improving readability and reusability. For more advanced concepts like templates, function pointers, and recursion, visit The Coding College.

Leave a Comment