Lecture overview -- Keyboard shortcut: 'u'  Previous page: Arrays -- Keyboard shortcut: 'p'  Next page: Procedures and Functions -- Keyboard shortcut: 'n'  Lecture notes - all slides together  Annotated slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Alphabetic index  Help page about these notes  Course home    Introduction to C# - slide 36 : 43

Procedures and Functions
Option Strict On
Option Explicit On
Module ArrayDemo

    Sub Sum(ByVal table() As Integer, ByRef result as Integer) 
      result = 0
      For i as Integer = 0 To 9
        result += table(i)
      Next
    End Sub

    Sub Main()
      Dim someNumbers(9) as Integer  
      Dim theSum as Integer = 0

      For i as Integer = 0 To 9
        someNumbers(i) = i * i
      Next
      Sum(someNumbers, theSum)
      Console.WriteLine(theSum)
    End Sub

End Module
using System;
class ProcedureDemo{

  public static void Sum(int[] table, ref int result){
    result = 0;
    for(int i = 0; i <= 9; i++)
      result += table[i];
  }

  public static void Main(){
    int[] someNumbers = new int[10];
    int theSum = 0;

    for(int i = 0; i <= 9;i++)
      someNumbers[i] = i * i;

    Sum(someNumbers, ref theSum);
    Console.WriteLine(theSum);
  }
}
Option Strict On
Option Explicit On
Module ArrayDemo

    Function Sum(ByVal table() As Integer) as Integer
      Dim result as Integer = 0 
      For i as Integer = 0 To 9
        result += table(i)
      Next
      return result
    End Function

    Sub Main()
      Dim someNumbers(9) as Integer  
      Dim theSum as Integer = 0
      For i as Integer = 0 To 9
        someNumbers(i) = i * i
      Next
      theSum = Sum(someNumbers)
      Console.WriteLine(theSum)
    End Sub

End Module
using System;
class ProcedureDemo{

  public static int Sum(int[] table){
    int result = 0;
    for(int i = 0; i <= 9; i++)
      result += table[i];
    return result;
  }

  public static void Main(){
    int[] someNumbers = new int[10];
    int theSum = 0;

    for(int i = 0; i <= 9;i++)
      someNumbers[i] = i * i;

    theSum= Sum(someNumbers);
    Console.WriteLine(theSum);
  }
}