0% found this document useful (0 votes)
3 views

CSCI1580-Lab4-Tutorial

The document outlines methods in Visual Basic (VB), focusing on subroutines and functions, including their differences and how to pass arguments by value or by reference. It explains the scope of variables and provides examples of subroutines and functions. Additionally, it includes an exercise on obtaining user input and manipulating numbers using these methods.

Uploaded by

3C 01 蔡依涵
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CSCI1580-Lab4-Tutorial

The document outlines methods in Visual Basic (VB), focusing on subroutines and functions, including their differences and how to pass arguments by value or by reference. It explains the scope of variables and provides examples of subroutines and functions. Additionally, it includes an exercise on obtaining user input and manipulating numbers using these methods.

Uploaded by

3C 01 蔡依涵
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Methods

YU Shaozuo
[email protected]

1
Objective
• Get familiar with methods in VB

2
Methods in VB
• Subroutine
• Function
• Pass-By-Value vs Pass-By-Reference

If there are some lines of code that you use


more than once, you can write with methods.

7
Subroutine vs. Function
• Difference between Subroutine and Function
– Function has a return value
– Example: print the sum of a and b
A simple subroutine:
Private Sub Add(ByVal a As Integer, ByVal b As Integer)
Console.WriteLine(a + b)
End Sub

A simple function:
Private Function Add(ByVal a As Integer, ByVal b As Integer) As
Integer
Return a + b ‘Add = a + b
End Function
How to run the above function? Console.WriteLine(Add(2,3))
8
ByVal vs. ByRef
• ByVal
– Sends a copy of the argument’s value
– The original value cannot be altered
• ByRef
– Sends a reference to the memory location where
the original value is stored
– The original value can be altered
Private Sub Add( ByVal a As Integer, ByVal b As Integer) Dim a As
End
Integer
Sub Console.WriteLine(a + b) Dim b As Integer
Add(1.4, 2) a=1, b=2 9
Scope of variables
• Scope refers to the visibility of variables.
– Determine where the variable can be used
– Namespace, Module level, Local, Block level

Namespace

Module Level

Local

Block Level

10
The block with the same indents is the

Scope of variables scope of this variable.

Module Module1
Dim result As Integer
Sub Main ()
Dim a, b As Integer
a = 1
b = 5
Try
Dim c As Integer = Console . ReadLine ( )
result = Discriminant( a, b , c)
Console . WriteLine ( " The discriminant is " & result &
".")
Catch
Console . WriteLine ( ” The input is invalid. " )
End Try
End Sub
Function Discriminant(ByVal a As Integer, ByVal b As Integer,
ByVal c As Integer) As Integer
Return b ^ 2 - 4 * a * c
End Function
End Module

11
Exercise
• Obtain valid user input

• Get the digits in tens and in ones.


– Subroutine
• Get the flip number. Try to avoid using
Console.Write/WriteLine
– Function statements in these two
methods.

12
Thanks!

You might also like