🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C++ Notes
Topic #77

Return Values


Return Values

The void keyword, used in the previous examples, indicates that the function should not return a value. If you want the function to return a value, you can use a data type (such as int, string, etc.) instead of void, and use the return keyword inside the function:

Example

int myFunction(int x) {
  return 5
+ x;
}

int main() {
  cout << myFunction(3);

return 0;
}

// Outputs
8 (5 + 3)

This example returns the sum of a function with two parameters:

Example

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

int main()
{
  cout << myFunction(5, 3);
  return 0;
}

// Outputs 8 (5 + 3)

You can also store the result in a variable:

Example

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

int main() {

int z = myFunction(5, 3);
  cout << z;
  return 0;
}
// Outputs 8 (5 + 3)

Pratical Example

Here is a simple and fun "game example" using a function with return to double a number five times:

Example

int doubleGame(int x) {
  return x * 2;
}

int main() {
  for (int i = 1; i <= 5; i++)
{
    cout << "Double of " << i << " is " <<
doubleGame(i) <<
endl;
  }
  return 0;
}

Want to go beyond the notes?

Join CodingNow 2.0's C++ course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Return Values – FAQs

Quick answers about learning Return Values in C++.

This free note from CodingNow 2.0 explains Return Values in C++ — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C++ topic on CodingNow 2.0, including Return Values, is 100% free with no signup required.
With focused practice, most students grasp Return Values in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now