- 1
What do we mean when we say that the function return a value? What is ' to return' really?
5 Réponses
+ 1
A function in the code, is based on the concept of math function, so almost the same ...
When you write it, you have a thing like this:
F (x) = 3x + 2
... which can be said: for each value of x, get the result of the expression after the equal sign.
Thus, in the code, we can perform a more complex procedure than a simple mathematical expression, and we do not always care about a result (in this case, the words "procedure" and / or "method" are more appropriate ). But when we want to get a result, we are talking about function by "returning" one. With some languages, you must specify the type of this result value, even if you do not return one (so you need a keyword like "void" to explain this fact) when you declare the function, on other not ^^
0
Function returns something that could be used later. When we define function we set what type of value it will return.For example: int giveIntPlease(){ int x; return x;}
0
A function is a programming construct that takes some arguments and compute a result. But it needs to return that result somehow, and that is where the return value comes in.
A simple example :
int min(int x, int y) {
if(x < y)
return x;
return y;
}
int z = min(6, 7); //z will be 6
0
The function returns a value into place in code, from where was the function called. Here can be assigned into any variables, or printed. Let see
int fun()
{....some code...;return 5;}
int a=fun(); //variable a has a value 5 returned from function
or
cout<<fun(); // a value 5 returned from function is printed
- 1
I'm not sure this answers your question to the specifity that you'd like, but to return a value essentially replaces the calling function and specific argument with the returned value: I.e. A prestructured function call is replaced by a specific return value.
If you'd like to learn really deeply into the language (and programming in general) I would recommend studying the C language, Assembly, and machine code, as they give great insight into how the higher level languages really 'work.'
Best of luck.