3
What is the return of a method in C#? When the method receives the parameters it generates a return depending on its structure, what would be the function of this generated return?
3
What is the return of a method in C#? When the method receives the parameters it generates a return depending on its structure, what would be the function of this generated return?
7
First you need to understand what a function is, and know that a method is just a function that has a hidden parameter called this
which receives the object you are using, unless it is static. You can read more about in What is the difference between functions and procedures? that says the function receives parameters, executes an algorithm and returns a value (if not returning a value is a procedure). You can also see Difference between a return and no return method.
In mathematics the function is precisely to produce a result. It is to have a formula that uses at various times the solution of a general problem, so instead of repeating the same formula for every place you can just call the function and it produces the result you want. In addition to getting a smaller code has a canonical information, which should calculate to get that result, in only one place, which is probably the principle that helps organize most important code that one should know, the one of the DRY.
Even if it is not just a procedure, it makes no sense not to produce a result. And producing the result has to send to those who called the function, this is the return.
That is why it is correctly said that the function generates a result, the return is mistakenly used when it is only the transport of that value.
The return, roughly, is the same mechanism of the parameter, only inverse, the parameter is the means of transport for whom called send a value to the function and the return is the same to carry back the function for whom called.
The return does not happen depending on the structure, the return value obviously may vary according to the executed algorithm.
The analogy used in the other answer is good, but it is wrong. Whoever calls the function is asking the question. The question can even be considered the header of the function, ie its name and parameters, the body of the function is the solution of the problem, ie, is how the answer to the question will be held, and finally will generate a result that is the answer of the question. The return is what determines how the answer will be given.
Another thing that may be of interest to the question and may not have been written is about the type of the function result, and therefore the type of the return. In general we say that the type of result is the type of function. It’s strictly not, a function has no type, but everyone understands it as being the kind of result.
One more point that could confuse is the command return
. It serves two things, determines the flow control that immediately terminates the execution of the function and can, most of the time, that is, in real functions, that a value is returned to the caller. You can see more in Why use only Return, without returning any data? and What "Return" does in Python?.
In C# it is possible to simplify a function of a one-line algorithm that only returns the result:
int Soma(int x, int y) => a + b;
Calling:
var resultado = Soma(1, 2);
So resultado
will have the value 3.
3
Quite simply, using an analogy, imagine that the method is a question and the return of the method is the answer. The implementation of the method would be the solution to arrive at the answer.
function somar(a, b) {
var resultado = a + b;
return resultado;
}
var resultadoDaSoma = somar(1, 2);
print soma; //3
Browser other questions tagged c# .net classes return
You are not signed in. Login or sign up in order to post.