Contenido del Curso
C# Basics
C# Basics
Method Return Values
In the last two chapters, we learned how to pass data into the functions but now we will learn how to retrieve data from the method back to the place where it was executed.
The process of retrieving data from methods is also called returning data and the data or value which is returned is called the return value.
The syntax for creating a method with a return value is the following:
main
The valueToReturn
represents a variable, value, or expression which must be of the same type as the returnDatatype
. Following is the correct example:
main
If the wrong type of data is returned, it will show an error:
main
The value which is returned from the sumOfThree
method can be stored into a variable:
main
We can also directly output the return value using Console.WriteLine
:
main
We can also directly write expressions as return values. In that case, the expression is first evaluated and then the resulting value is returned. Following are some examples:
Product of Three Integers:
main
Average:
main
¿Todo estuvo claro?