0
What is the purpose and meaning of a return command in C++?
It says a return command returns a value, what does it mean to return a value? Also, what is the value being returned and whatâs the purpose and function of a return command. How is this different than simply outputting a function or command?
1 Answer
+ 2
The computer normally executes a program sequentially for example if the program does 4 different tasks, they can go like
a b c d
But usually a program will do the same operation multiple times. It is important to avoid writing the same code twice. This is known as DRY principle (don't repeat yourself). For this reason we organize some recurring parts of our code in FUNCTIONS. So if task b is repeated then the program might execute like this:
a b c b d b
A function can have a fixed "recipe" but different inputs will cause different outputs. Return means that the output is communicated back to the part of the program, where the function was executed.
By output, I don't mean any printing, just the result of a calculation for example.