This is your first visit inside The Matrix. Welcome!

The Matrix has you…

This place will take you into the mirage of learning C# programming language, without any previous coding experience being required

Follow the White Rabbit…█

Close
Tuesday, July 17, 2018 07:43

Return operator

In few of our previous lessons, we used functions – methods that not only can be called, perform some action, but also return a value to the caller – the piece of code that called them. You can imagine this as placing the returned value in the place where the function was invoked from. We can:

  • assign to a variable – the returned value can directly be assigned to a variable that has the same type as the return type of the function:
  • use in expressions – once a function returns a value, that value can directly be used in other expressions and calculations:
  • use it as method parameters – it is perfectly valid to use the returned value of a function as a parameter for another method or function call:

As I explained in a few previous lessons, what differentiates a method from a function is the fact that the latter always return a value, and must always use a return statement. The basic blueprint for a function is this:

Always remember that the returned value must be of same type with the function’s return type that you declared. You cannot have this:

So, if we declared our function as returning a float value, it must return a float value. Otherwise, the compiler will give us an error: Cannot implicitly convert type ‘double’ to ‘float’.

However, there are a small cases in which returning a different type of value from the declared return type is allowed. Consider this:

To your surprise, even if our function has its signature declared as returning a float, we are returning an integer, and the compiler will not complain about it. This is because even if the returned type is not of type float, it can implicitly be converted to float.

However, it would be best to stick with the rule of returning the same type as the function declares in its signature, just to avoid confusion.

Another thing that you should be aware is what happens with the code that follows after the return keyword. Have a look at the following example:

Some of you may expect that “some other code here” will be executed, as we learned that the execution continues from top to bottom in a linear way. This is actually wrong, because the return keyword will immediately end the execution of the function, and return the value we are providing it, in our example, “Hello World!”. All the code that follows after the return keyword will not be executed. In fact, the compiler will even warn us about this: Unreachable code detected. In plain words, this means that that particular piece of code will never be executed, because the return keyword will end the function’s execution and return its result.

We are also allowed to directly return an expression:

In this case, the compiler will first evaluate the result of the x * y expression, and only then return the resulting value. It is preferred to avoid this when the return instruction becomes complicated or long.

How about methods using the return keyword? Are they allowed?

The answer is yes, but not in the form you would imagine it. Yes, I know I said many times that methods do not return a value, and functions do, but before you point your finger at me, look at the following example:

So, we have a method for deleting a file, with a string parameter where we specify the actual location of the file we want to delete. What would happen if we would try to delete a file that no longer exists? The program would most likely generate an error. This is why we first check if the said file exists, and if it doesn’t (remember the “negation” boolean operator “!”? In this case it translates as “if file NOT exists”), then use the return keyword. Just like for functions, the return operator will immediately stop the execution of our method and return the execution workflow to where it was before the method call. The only difference between a method and a function is that a method cannot return a value. We cannot return an int, or a float, or anything. That is why, we can only use the return keyword followed by no other instruction. In this case, we are using it just to immediately stop the execution of our method and return to where the method was called.

What would happen if our function’s code is really complex, with a lot of branches? How do we use the return keyword in multiple places?

The above code takes two integer parameters and performs some checks with them. If the first is greater than the second, it returns a value. If they are equal, it returns some other value, and so on. That is perfectly valid, and we are allowed to have multiple return statements because they are separated by the functionality of the if else statement. As we already know, we cannot have two parts of an if else statement execute; it is either one, or the other. This is what allows us to return multiple values in multiple places, and this is also what takes us in a dangerous area. Consider the following example:

To your surprise, the above code will generate a compiler error: ‘Program.CompareNumbers(int, int)’: not all code paths return a value. Why is that? Well, let’s think it through: like in the previous example, we have a function with two integer parameters, and we perform some checks on them. If the first is greater than the second, we return 1. If they are equal, we return 0. And……. What if the first is less than the second? This is what generates the error, the compiler knows that there is this additional case which we are not checking, and because a function must return a value, it will generate an error, since we are not returning a value for the case when x is less than y. In conclusion, when you see the above error, always check for cases that can happen, and you are not implementing them.

Comments

comments

Tags: , , , , ,

2 Responses to “Return operator”

  1. psimard says:

    Could we use “break” instead of return to get out of a method?

    • rusoaica says:

      No, not really. The break operator can only be used inside loops and in Switch statements.

Leave a Reply