In this chapter you will learn:
- What is the return statement in C#?
- What is the functionality of return statement?
- How to use return statement in programming?
A return statement is used for returning value to the caller from the called function. An easy example is mentioned below in a program that demonstrates the return statement very clearly.
Example:using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace return_statements { class Program { public int add(int num1, int num2) { // returns the add of num1 and num2 return num1 + num2; } static void Main(string[] args) { Program p = new Program(); int result; // calling the function add that will return 9 to the result vaiable. result = p.add(3, 6); Console.WriteLine(result); Console.ReadLine(); } } }
In this example the variable result calls the function add() with two parameters and the function add() returns addition of both number to the result variable using return keyword.
Output
9
__
Summary
In this chapter you learned what return statement is in C# and how to use it in programming. In next chapter you will learn throw statement in C#.