function Return

In this chapter you will learn:

  1. How to return value from a function
  2. How to return an object value from a method
  3. How to return a value of an array from a method

Return value from a function

Methods return a value to the calling routine using this form of return:

return value;

value is the value returned.

using System;/*j  a  v a  2 s .  co m*/

class MainClass
{
   public static int GetHour()
   {
      return 123;                   
   }

   static void Main()
   {
      Console.WriteLine("Hour: {0}", GetHour());
   }
}

The code above generates the following result.

Return an object

The following code defines a method and return its instance from a method.

using System; //  j  av  a  2s . c o m
 
class Rect { 
  int width; 
  int height; 
 
  public Rect(int w, int h) { 
    width = w; 
    height = h; 
  } 
 
  public int area() { 
    return width * height; 
  } 
 
  public void show() { 
    Console.WriteLine(width + " " + height); 
  } 
 
  public Rect enlarge(int factor) { 
    return new Rect(width * factor, height * factor); 
  } 
} 
  
class MainClass { 
  public static void Main() {   
    Rect r1 = new Rect(4, 5); 
 
    Console.Write("Dimensions of r1: "); 
    r1.show(); 
    Console.WriteLine("Area of r1: " + r1.area()); 
 
    Console.WriteLine(); 
 
    // create a rectangle that is twice as big as r1 
    Rect r2 = r1.enlarge(2); 
 
    Console.Write("Dimensions of r2: "); 
    r2.show(); 
    Console.WriteLine("Area of r2 " + r2.area()); 
  } 
}

The code above generates the following result.

Return an array

The following code defines a method returning array.

using System; // j a v a 2s .  c  om
 
class Factor { 
  public int[] findfactors(int num, out int numfactors) { 
    int[] facts = new int[80]; 
    int i, j; 
 
    for(i=2, j=0; i < num/2 + 1; i++)  
      if( (num%i)==0 ) { 
        facts[j] = i; 
        j++; 
      } 
     
    numfactors = j; 
    return facts; 
  } 
} 
  
class MainClass { 
  public static void Main() {   
    Factor f = new Factor(); 
    int numfactors; 
    int[] factors; 
 
    factors = f.findfactors(1000, out numfactors); 
 
    Console.WriteLine("Factors for 1000 are: "); 
    for(int i=0; i < numfactors; i++) 
      Console.Write(factors[i] + " "); 
       
    Console.WriteLine();    
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to write recursive methods
  2. Display a string in reverse by using recursion
  3. How to calculate fibonacci value in C#
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances