Return Value Functions

COS1511 - Introduction To Programming I · Learning C++

Return Value Functions

In C++, a function can return a value to the part of the program that called it. This is known as a return value function. Understanding how to create and use return value functions is crucial for effective programming.

Defining a Return Value Function

To define a return value function, you need to specify the return type, the function name, and the parameters (if any). The return type indicates the type of value the function will return. Here is the general syntax:

return_type function_name(parameter_type parameter_name) {
    // function body
    return value;
}

For example, consider a function that adds two integers and returns the result:

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

In this example, the return type is int, meaning the function will return an integer value. The function add takes two parameters, a and b, which are both integers. The function body adds these two integers and returns the sum.

Using a Return Value Function

To use a return value function, you call it from another part of your program. You can store the returned value in a variable or use it directly in an expression. Here is an example:

int main() {
    int result = add(5, 3);
    std::cout << "The sum is: " << result;
    return 0;
}

In this code, we call the add function with the arguments 5 and 3. The function returns the value 8, which is stored in the variable result. The program then prints this value.

Remember: Always ensure that the return type of the function matches the type of the value you are returning.

Return Value Functions with Different Data Types

Return value functions can return different types of data. Here are a few examples:

Returning a Floating-Point Number

You can create a function that returns a floating-point number:

double divide(int numerator, int denominator) {
    if (denominator == 0) {
        std::cout << "Cannot divide by zero!";
        return 0.0;
    }
    return static_cast(numerator) / denominator;
}

This function takes two integers, numerator and denominator, and returns their division as a double. It also handles the case where the denominator is zero by printing a message and returning 0.0.

Returning a Boolean Value

A function can also return a boolean value. For example:

bool isEven(int number) {
    return number % 2 == 0;
}

This function checks if a number is even. It returns true if the number is even and false otherwise.

Watch out: Be careful when returning values from functions. Make sure the return type matches the expected type in the calling code.

Returning Values from Functions with Parameters

When a function takes parameters, you can use these parameters to calculate the return value. For example:

int multiply(int x, int y) {
    return x * y;
}

This function multiplies two integers and returns the result. You can call it like this:

int main() {
    int product = multiply(4, 5);
    std::cout << "The product is: " << product;
    return 0;
}

In this example, the function multiply is called with 4 and 5, returning 20.

Multiple Return Statements

A function can have multiple return statements. However, only one return statement will execute during a single function call. For example:

int checkNumber(int number) {
    if (number > 0) {
        return 1; // Positive number
    } else if (number < 0) {
        return -1; // Negative number
    }
    return 0; // Zero
}

This function checks if a number is positive, negative, or zero. It returns 1 for positive numbers, -1 for negative numbers, and 0 for zero.

Tip: Use return statements wisely. Ensure that all code paths return a value if the function is defined to return a value.

Practical Example

Let us put everything together in a practical example. This program will calculate the area of a rectangle:

double calculateArea(double length, double width) {
    return length * width;
}

int main() {
    double length = 5.0;
    double width = 3.0;
    double area = calculateArea(length, width);
    std::cout << "The area of the rectangle is: " << area;
    return 0;
}

In this program, the calculateArea function takes two parameters, length and width, and returns the area of the rectangle. The main function calls calculateArea with the values 5.0 and 3.0, and prints the result.

Summary

  • A return value function specifies a return type and returns a value to the caller.
  • You can define functions to return different data types, such as integers, doubles, or booleans.
  • Functions can have multiple return statements, but only one will execute per call.
  • Always ensure that the return type matches the value being returned.

Check your understanding

  1. What is the purpose of a return value function in C++?
  2. How do you define a function that returns a double?
  3. What happens if you try to return a value of a different type than the specified return type?
  4. Can a function have multiple return statements? Explain your answer.