Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7 Methods (functions and subroutines) Parameter Passing

Similar presentations


Presentation on theme: "Lecture 7 Methods (functions and subroutines) Parameter Passing"— Presentation transcript:

1 Lecture 7 Methods (functions and subroutines) Parameter Passing
Scope of Variables ByVal or ByRef

2 Agenda: To understand the need for methods
To know how to write and call methods To know how to pass parameters to a method ByRef vs. ByVal for parameters passing Scope of variables

3 What is a method? A method is self-contained block of code that performs some operations Methods break the program up and make it more understandable Methods promote code reuse DRY – Don’t Repeat Yourself

4 Why use methods? Problem
You want to have same message box titles throughout the application.

5 If rbnLess21.Checked Then
MessageBox.Show("Sorry,…", "Cruel Games") ElseIf rbn21to35.Checked Then MessageBox.Show("Let’s rock", "Cruel Games") Else MessageBox.Show("You…", "Cruel Games") End If

6 But what if you want to change the title of the message boxes?

7 You should create method
Private Sub ShowMessage (ByVal text As String) MessageBox.Show(text, "Cruel Games") End Sub

8 Functions And what if you want to have consistency among your InputBoxes?

9 The code… Private Function GetInput(ByVal Message As Double) As String
Return InputBox(text, “Marks") End Function

10 Sub and Function VB.NET can define a method using Sub keyword or using Function keyword Sub is used when the method does not return a value and is short for subroutine Function is used when the method does return the value

11 Calling a method You can call a method within a block in your program by specifying the name of function and parameters in parenthesis (or empty parenthesis) Dim mark As Integer = GetInput(“Enter the mark”) ShowMessage(“Let’s rock”)

12 ByVal vs ByRef

13 What will be printed? Sub Main() dim A as Integer A = 5 DoSmt(A)
Debug.Write(A) ‘? End Sub Sub DoSmt(ByVal B as Integer) B = B + B

14 Sending by Value When we send the parameters to the function by value the called function creates a local copy of that variable and any changes to that variable will have no effect on the original variable. It is safe to send by value to avoid accidental change to the variable

15 The Answer is 5 … 5 5 10 Why? Main A DoSmt(By Val b As Integer)

16 Sending By Reference When parameter sent by reference the called function does not create a local copy of the original variable but it creates the reference to it. So changes applied to variable in the called function will effect the variable in the calling function. Used when we need to change the value of the variable in the function

17 What will be printed? Sub Main() dim A as Integer A = 5 DoSmt(A)
Debug.Write(A) ‘? End Sub Sub DoSmt(ByRef B as Integer) B = B + B

18 The Answer is 10 Why? Main() 5 A DoSmt(By Ref B As Integer) B

19 Scope

20 Variable Scope You can't define a variable at any spot in the program and use it at any spot, there is a specific relationship between where a variable is defined and where it can be used. This is known as the scope of the variable.

21 Variable Scope Block begins with an opening statement (For, If, Sub…) and ends with a closing one (usually Next, End If, End Sub…) Block defines a scope Variable declared inside the outer block will be visible in the inner block, but not vise-versa.

22 Example Dim y As Integer = 56 For i As Integer = 0 To 9
y = y – 1 ‘possible - y is visible in the inner block Next If (y < 3) Then Dim x As Integer = 9 End If x = 8 ‘impossible - x is defined in the inner block and is not accessible in the outer block

23 Variable Lifetime Variables are valid only after their declaration
Variables are destroyed when their scope is finished Variables declared within a block will be destroyed when their block finishes execution

24 Example Dim y As Integer = 56
For i As Integer = 0 To 9 ‘i is created here y = y – 1 Next ‘i is destroyed at this point If (y < 3) Then Dim x As Integer = 9 ‘x is created here MsgBox("X = " & x) End If ‘x is destroyed at this point

25 The End


Download ppt "Lecture 7 Methods (functions and subroutines) Parameter Passing"

Similar presentations


Ads by Google