Return Types
In this page:
Return Types
A method's return type declares what kind of value it hands back to its caller with the return keyword. A method that returns nothing uses void. Every code path in a non-void method must return a value, or the compiler raises an error.
Warning: Leaving out a return on some code path of a non-void method is a compile error, not a runtime one.
Example: Return Types
using System;
class Program
{
static int Square(int n)
{
return n * n;
}
static void Main(string[] args)
{
int result = Square(6);
Console.WriteLine(result);
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: