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

Зміст курсу

C# Basics

Method Return ValuesMethod 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.cs

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.cs

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

cs

main.cs

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

cs

main.cs

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

cs

main.cs

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.cs

Average:

cs

main.cs

What should be the return value of the following method?

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

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

Секція 6. Розділ 6
course content

Зміст курсу

C# Basics

Method Return ValuesMethod 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.cs

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.cs

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

cs

main.cs

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

cs

main.cs

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

cs

main.cs

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.cs

Average:

cs

main.cs

What should be the return value of the following method?

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

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

Секція 6. Розділ 6
some-alt