Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedures: Functions and Subroutines

Similar presentations


Presentation on theme: "Procedures: Functions and Subroutines"— Presentation transcript:

1 Procedures: Functions and Subroutines

2 Procedures Functions and Subroutines are Procedures.
Procedures are discreet blocks of code that perform discreet tasks.

3 Arguments Arguments are the values passed into a function or subroutine A function or subroutine may have: No arguments A number of arguments An variable number of arguments

4 Functions Functions differ from subroutines in one key aspect: Functions return a value. Although functions and subroutines may change any number of variable values, a function returns the value processed by the formula or computation.

5 A Function: A Magic Box myFunction = A = 10 myFunction = A = 12
Console.WriteLine(myFunction(R, S, T)) Console.WriteLine(myFunction(A, B,C)) R= 3 S= 2 T = 1 A= 2 B= 3 C = 4 myFunction = A = 10 Function myFunction( X As Integer, Y As Integer, Z as Integer ) A = X ^ Y + Z Return A End Function myFunction = A = 12

6 ByRef vs. ByVal Function myFunction(ByVal X as integer)
ByVal sends the value into the function ByRef sends the reference to the actual memory space into the function. THUS ByRef CAN CHANGE THE ACTUAL MEMORY LOCATION OF THE VARIABLE PASSED INTO THE FUNCTION …even though the variable name is different. ByRef is the default

7 Console.WriteLine("X = " & x) Console.WriteLine("Enter New X Value:")
This refers to the actual memory location. Y=Y+1 changes the value of X Sub Main() Dim x As Integer Do While x < 100 Console.WriteLine("X = " & x) Console.WriteLine("Enter New X Value:") x = Console.ReadLine() doSomething(x) Loop End Sub Sub doSomething(ByRef y As Integer) y = y + 1


Download ppt "Procedures: Functions and Subroutines"

Similar presentations


Ads by Google