Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Method Return Values | Methods
C# Basics
course content

Зміст курсу

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:

cs

main

The valueToReturn represents a variable, value, or expression which must be of the same type as the returnDatatype. Following is the correct example:

cs

main

If the wrong type of data is returned, it will show an error:

cs

main

The value which is returned from the sumOfThree method can be stored into a variable:

cs

main

We can also directly output the return value using Console.WriteLine:

cs

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:

cs

main

Average:

cs

main

What should be the return value of the following method?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 6. Розділ 6
We're sorry to hear that something went wrong. What happened?
some-alt