C++ User-defined Function Types


Introduction

In C++, a user-defined function is a function that you define to perform a specific task in your program. These functions can help make your code more modular, reusable, and easier to understand. User-defined functions are crucial for breaking complex problems into smaller, manageable pieces.

There are different types of user-defined functions based on their return types, parameters, and whether they perform any operations or simply return values. In this blog, we'll explore the following types:

  • Void functions: Functions that do not return any value.
  • Returning functions: Functions that return a value after performing an operation.
  • Functions with parameters: Functions that accept inputs to perform actions.
  • Functions with and without default arguments.

1. Void Functions (Functions with No Return Type)

A void function is one that doesn't return any value. It can be used when you want to perform an action, such as printing something to the screen or modifying a variable, but you don't need to return any result.

Example 1: Void Function

#include <iostream>
using namespace std;

// Void function definition
void displayMessage() {
    cout << "Hello, this is a user-defined function!" << endl;
}

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

Output:

Hello, this is a user-defined function!

Explanation:

  • The function displayMessage() is a void function because it does not return any value. It simply prints a message.
  • void is used as the return type to indicate that no value will be returned from the function.

2. Returning Functions (Functions That Return a Value)

A function that returns a value performs some operation and sends a result back to the calling code. You define the return type of the function based on the type of value you want to return.

Example 2: Returning Function

#include <iostream>
using namespace std;

// Function that returns an integer value
int addNumbers(int a, int b) {
    return a + b;
}

int main() {
    int result = addNumbers(10, 5);  // Function call
    cout << "The sum is: " << result << endl;
    return 0;
}

Output:

The sum is: 15

Explanation:

  • The function addNumbers() takes two integer parameters and returns their sum.
  • The return type of the function is int, meaning it will return an integer value to the calling function.

3. Functions with Parameters (Accepting Input from the User)

Functions can accept parameters (input values) that are passed when calling the function. These values are used inside the function to perform a task.

Example 3: Function with Parameters

#include <iostream>
using namespace std;

// Function with parameters to calculate the area of a rectangle
double calculateArea(double length, double width) {
    return length * width;
}

int main() {
    double area = calculateArea(5.0, 3.0);  // Function call with parameters
    cout << "Area of the rectangle is: " << area << endl;
    return 0;
}

Output:

Area of the rectangle is: 15

Explanation:

  • The function calculateArea() takes two parameters: length and width, and returns their product (the area of the rectangle).
  • The function is called with arguments 5.0 and 3.0 representing the length and width of the rectangle.

4. Functions with Default Arguments

In C++, you can define default arguments for function parameters. This allows you to call the function with fewer arguments than the function definition provides, and the missing arguments will automatically take their default values.

Example 4: Function with Default Arguments

#include <iostream>
using namespace std;

// Function with default arguments
double calculateArea(double length, double width = 1.0) {
    return length * width;
}

int main() {
    double area1 = calculateArea(5.0);    // Call with one argument
    double area2 = calculateArea(5.0, 3.0); // Call with both arguments

    cout << "Area with default width: " << area1 << endl;
    cout << "Area with specified width: " << area2 << endl;

    return 0;
}

Output:

Area with default width: 5
Area with specified width: 15

Explanation:

  • The function calculateArea() defines width with a default value of 1.0.
  • If the caller provides only one argument (like in the first call), width defaults to 1.0.
  • If both arguments are provided, the default value is ignored.

5. Overloaded Functions (Functions with Same Name but Different Parameters)

C++ allows function overloading, which means you can define multiple functions with the same name but with different parameter types or numbers. This helps in performing similar operations with different types of data.

Example 5: Function Overloading

#include <iostream>
using namespace std;

// Overloaded function to calculate area of a rectangle
double calculateArea(double length, double width) {
    return length * width;
}

// Overloaded function to calculate area of a square
double calculateArea(double side) {
    return side * side;
}

int main() {
    double area1 = calculateArea(5.0, 3.0);  // Rectangle
    double area2 = calculateArea(4.0);      // Square

    cout << "Rectangle area: " << area1 << endl;
    cout << "Square area: " << area2 << endl;

    return 0;
}

Output:

Rectangle area: 15
Square area: 16

Explanation:

  • The function calculateArea() is overloaded with two versions: one that calculates the area of a rectangle (two parameters) and one that calculates the area of a square (one parameter).
  • The correct version of the function is called based on the number and types of arguments passed.

6. Recursion in Functions

Recursion is a concept where a function calls itself. Recursive functions are useful for problems that can be broken down into smaller subproblems, such as calculating the factorial of a number or traversing a tree.

Example 6: Recursive Function

#include <iostream>
using namespace std;

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

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

Output:

Factorial of 5 is: 120

Explanation:

  • The function factorial() calls itself to compute the factorial of a number.
  • The base case is n == 0, which returns 1. For any other value of n, the function continues to call itself until n reaches 0.