C++ Functions Return Statement

The return statement in C++ is used to exit a function and return a value to the calling function. The return statement is often used to end the execution of a function early, before it reaches the end of the function. It can also be used to return a value from the function to the calling code.

Returning a Value

Functions in C++ can return a value to the calling code. The type of the value returned must match the return type of the function. Here is an example of a function that takes two integers and returns their sum:

int add(int x, int y) {
    return x + y;
}

This function takes two integers as parameters and returns the sum of those integers. The return type of the function is int, so the value returned by the function must also be an int.

The returned value can be assigned to a variable or used in an expression:

int a = 5, b = 7;
int result = add(a, b);
cout << result;

// Output: 12

Ending a Function Early

The return statement can also be used to end the execution of a function early, without reaching the end of the function. Here is an example of a function that takes an integer as a parameter and returns “even” if the integer is even and “odd” if the integer is odd:

string evenOrOdd(int x) {
    if (x % 2 == 0) {
        return "even";
    }
    else {
        return "odd";
    }
}

In this example, the function takes an integer as a parameter and checks if it is even or odd. If the number is even, the function returns the string “even” and the execution of the function ends. If the number is odd, the function returns the string “odd” and the execution of the function ends.

Returning Multiple Values

C++ does not support returning multiple values from a function directly. However, there are several ways to achieve this. One way is to use reference parameters to return multiple values. Here is an example:

void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}

In this example, the swap function takes two integers as reference parameters and swaps their values. The values of the original variables are modified and returned to the calling code.

Another way is to use a struct or a class to return multiple values. Here is an example:

struct Point {
    int x;
    int y;
};

Point getPoint() {
    Point p;
    p.x = 5;
    p.y = 7;
    return p;
}

In this example, the getPoint function returns a Point struct containing two integers x and y. The values of x and y are returned to the calling code.

Returning Void

If a function does not return a value, its return type is void. Here is an example of a function that takes an array of integers and prints its elements:

void printArray(int *arr, int size) {
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

In this example, the printArray function takes an array of integers and its size as parameters, and prints the elements of the array. The return type of the function is void, because it does not return a value to the calling code.

Returning Expressions

The value returned by a function can be an expression. Here is an example of a function that takes two integers and returns the larger one:

int max(int x, int y) {
    return (x > y) ? x : y;
}

In this example, the max function takes two integers as parameters and uses the ternary operator (? 🙂 to return the larger of the two integers. The value returned is the result of the expression (x > y) ? x : y.

Returning Const

A function can return a const value or a const reference. This is useful when returning an object that should not be modified by the calling code. Here is an example of a function that returns a const reference to a string:

const string& getName() {
    static string name = "John Doe";
    return name;
}

In this example, the getName function returns a const reference to a string. The returned string is a static variable, so it is stored in the data segment of the program and has a lifetime equal to the lifetime of the program. Attempting to modify the returned string will result in a compile-time error.

Conclusion

The return statement in C++ is a powerful tool that allows a function to return a value to the calling code, end its execution early, or return multiple values using reference parameters or structs. Return types can also be void if a function does not return a value, or const if the returned value should not be modified by the calling code.

Leave a Reply

Your email address will not be published. Required fields are marked *