C++ from Scratch to Advance / Functions in C++ / Value-Returning Functions in C++
Home /C++ from Scratch to Advance /Functions in C++ /Value-Returning Functions in C++

Value-Returning Functions in C++

When you are learning how to learn C++ step by step, you eventually move beyond functions that just “do” something (like printing a message) to functions that “give” something back. In our previous lessons, we looked at void functions. Today, we dive into Value-Returning Functions, the workhorses of professional programming that allow different parts of your code to communicate and share data.

Definitions

Before we explore the logic, let’s define our terms simply:

Term Definition / Description
Value-Returning Function A function that completes its task and then sends a piece of data back to the part of the program that called it.
Return Statement The specific line of code (return) that actually “hands over” the result to the calling program.
Return Type The category of data (like int, float, or char) that the function is promised to give back.
Calling Part The place in your code (usually int main) where you ask the function to start working.
Data Type Matching The strict rule that the value you return must be the same type as the one you promised in the function’s header.

The Concept: Giving and Taking

In previous lectures, we used void functions. A void function can take information in, but it never hands anything back; it’s like a one-way street. A Value-Returning Function, however, is a two-way street. It can accept “raw materials” (parameters), process them, and then provide a “finished product” (the return value).

Relatable Daily Life Example:

Think of a value-returning function like using a blender machine:
  • The Input (Arguments): You add some fruit and press the button.
  • The Operation (Body): The blender starts blending the fruits and makes the juice.
  • The Output (Return Value): At the end you get juice as output or in return.
  • The Calling Part: You are the “main program.” You gave something, waited for the work to be done, and now you are holding a result that you can either drink immediately or store in your fridge for later.

The Syntax: How to Write It

The structure is almost identical to a void function, with two major changes: the Data Type and the Return Statement.
  1. Data Type: Instead of writing void, you write the type of data you are returning (e.g., int, double, char).
  2. The Return Statement: At the end of the function body, you use the return keyword followed by the value or expression you want to send back.

Example Format:

int sumFunction(int a, int b) {
    int sum = a + b;
    return sum; // Handing the total back to the main program
}

Crucial Rule: The Type Matching Requirement

One of the most common errors for students is a mismatch in data types. If you tell the compiler your function is an int type, but you try to return a char (character), the program will fail.
  • If the function header says int, the value returned must be an integer.
  • If the header says float, you must return a floating-point number.
  • You can return a simple variable, or even a full mathematical expression like return 3 + 2;.

How to “Call” a Value-Returning Function

This is where beginners often get stuck. Because these functions “hand you” a value, you can’t just call them by name like you do with void functions. If you just write sumFunction(2, 3);, the value is returned, but it “falls on the floor” because nobody is catching it.
To use the returned value, you have two main options:
  1. Store it: Create a variable in your main function to “catch” and hold the value.
  2. Print it: Use cout immediately to show the result on the screen.

Practical Implementation: The Summation Program

In the video, we see a program that adds two numbers. The function sum1 takes two integers, adds them, and returns the result.
#include <iostream>
using namespace std;

// Function Definition
int sum1(int a, int b) {
    int sum = a + b;
    return sum; // Returns the integer total
}

int main() {
    // Calling the function and storing the result in a variable
    int result = sum1(2, 3); // 'result' now holds the value 5 
    
    cout << "The sum is: " << result << endl; // Printing the stored value
    
    return 0;
}

Interactive Elements: Try It Yourself!

  1. Reflection Prompt: Why do you think a void function cannot use a return statement to send a number back to main?
  2. Challenge 1: Write a value-returning function that takes one number and returns its square (the number multiplied by itself).
  3. Challenge 2: Try creating a function with a double return type. What happens if you try to return a whole integer?
  4. Challenge 3: Modify the Summation program above to take input from the user (using cin) instead of using the fixed numbers 2 and 3.
  5. Logic Test: What happens if you have two return statements in the same function? (Hint: The function stops the moment it hits the first one!)

Take the Next Step

Ready to see this logic in action with more complex parameters?, check out the official C++ Documentation on Return Statements.
Happy Coding!
Video Tutorial