0
0
C++programming~15 mins

Return values in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
Return values
📖 Scenario: Imagine you are creating a simple calculator program that adds two numbers and returns the result.
🎯 Goal: Build a program that defines a function to add two numbers and returns the sum. Then, use this function to get the result and display it.
📋 What You'll Learn
Create a function called add that takes two integers as parameters
The add function must return the sum of the two integers
Call the add function with the numbers 7 and 5
Store the returned value in a variable called result
Print the value of result
💡 Why This Matters
🌍 Real World
Functions that return values are used everywhere in programming to get results from calculations or data processing.
💼 Career
Understanding return values is essential for software development jobs, as it helps you write clear and reusable code.
Progress0 / 4 steps
1
Create the add function
Write a function called add that takes two integers named a and b and returns their sum.
C++
Need a hint?

Define a function with return type int named add that returns a + b.

2
Call the add function and store the result
Inside the main function, call the add function with arguments 7 and 5 and store the returned value in an integer variable called result.
C++
Need a hint?

Use int result = add(7, 5); inside main.

3
Print the result
Add a line inside main to print the value of result using std::cout.
C++
Need a hint?

Use std::cout << result << std::endl; to print the value.

4
Run the program and see the output
Run the program. It should print the sum of 7 and 5, which is 12.
C++
Need a hint?

The program should print 12 because 7 + 5 = 12.