We already examined methods that do a specific task, for example printing some text, a number or a figure on the console. There are also other types of methods that can return some kind of result. These are the methods we are going to analyze in the next lines.
Up until now we saw some examples, in which when declaring methods, we used the keyword void
, which shows that the method does not return a result, but just executes a certain action.
If we replace void
by a type of some variable, this will tell the program that the method should return a value of the said type. This returned value could be of any type – int
, string
, double
etc.
In order for a method to return a result we need to write the type of returned value we expect when declaring the method, in the place of void . |
We should note that the result returned by the method can be of a type, compatible with the type of the returned value of the method. For example, if the declared type of the returned value is double
, we can return a value of int
type.
In order to obtain a result from the method we need to use the return
operator. It should be used in the body of the method and tells the program to stop its execution and to return the method invoker a certain value, which is defined by the expression after the return
operator.
In the example below there is a method that reads two names from the console, concatenates them and returns them as a result. The return value is of string
type:
The return
operator can also be used in void
methods. This way the method will stop its execution without returning a value, and after it there shouldn't be an expression, which should be returned. In this case we use return
only to exit the method.
There are cases where return
can be called from multiple places in the method, but only if there are certain input conditions.
We have a method in the example below, which compares two numbers and returns a result respectively -1
, 0
or 1
depending on if the first argument is smaller, equal or larger than the second argument, given to the function. The method uses the keyword return
in three different places, in order to return three different values according to the logic of comparing the numbers:
We can invoke the above method and ensure that it behaves as expected:
- CompareTo(3, 4) → -1
- CompareTo(20, 10) → 1
- CompareTo(5, 5) → 0
- CompareTo(-5, -7) → 1
- CompareTo(-10, -5) → -1
After the return
operator, there should not be any more lines of code in the current block, because if there are, Visual Studio will warn you that it has found a piece of code that is inaccessible:
After a method is executed and returns a value, this value can be used in multiple ways.
The first one is to assign the result as a value of a variable of a compatible type:
The second one is for the result to be used in an expression:
The third one is to pass the result of the method to another method:
Create a method that calculates the area of a triangle using the given base and height and returns it as a result.
Input | Output |
---|---|
3 4 |
6 |
The first step is to read the input. After that, create a method, but this time be careful when declaring to give it the correct type of data we want the method to return, which is double
.
The next step is to call the new method from the Main()
method and to store the returned value in a suitable variable.
Test your solution here: https://judge.softuni.org/Contests/Practice/Index/594#4.
Create a method that calculates and returns as result the power of a given number.
Input | Output | Input | Output |
---|---|---|---|
2 8 |
256 | 3 4 |
81 |
Our first step is to read the input data from the console. The next step is to create a method that will take two parameters (the number and the power) and returns as a result a number of double
type.
After we have done the calculations, we have to only print the result in the Main()
method of the program.
Test your solution here: https://judge.softuni.org/Contests/Practice/Index/594#5.