1 inf110 visual basic programming aubg spring semester 2011 reference books: schneider d., an...

50
1 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice Hall, Pearson Education Inc., 7th Ed. 2008, 6th Ed. 2006 Liberty J., Learning Visual Basic .NET, O’Reilly, 2002 Any Visual Basic book available in AUBG library Course lecturer: Assoc. Prof. Svetla Boytcheva, PhD

Upload: patrick-chandler

Post on 25-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

1

INF110Visual Basic Programming

AUBG Spring semester 2011

Reference books:Schneider D., An Introduction to Programming Using Visual Basic, Prentice Hall, Pearson Education Inc., 7th Ed. 2008, 6th Ed. 2006 Liberty J., Learning Visual Basic .NET, O’Reilly, 2002 Any Visual Basic book available in AUBG library

Course lecturer: Assoc. Prof. Svetla Boytcheva, PhD

Page 2: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

2

INF110 Visual Basic Programming AUBG Spring semester 2011

Lecture 11Title:

Visual Basic(Procedures2: Sub-s &

Function-s)

Page 3: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

3

Lecture Contents:

Reminder – basic ConceptsByVal parametersByRef parametersDemo programsPractical session

Page 4: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

4

Page 5: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

5

ProceduresProcedures

Demo programs by Demo programs by J.Liberty,J.Liberty,

Chapter 06Chapter 06

06010601

INF110

Visual Basic Programming

Page 6: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

6

Reminder

Page 7: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

7

Basics of procedures:

“Properly designed functions permit to ignore how a job’s done. Knowing what is done is sufficient.”

B.Kernighan & D.Ritchie

“A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation. ”

B.Kernighan & D.Ritchie

Page 8: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

8

Devices for modularity

• VB.NET has two constructs for dividing problems into smaller pieces (sub-problems)

–Sub procedures

–Function procedures

They are blocks of code that can be referred to by a name.

Page 9: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

9

Sub Procedures and Function Procedures

- Two activities are must for effective processing with Sub Procedures and Function Procedures in VB:

- 1. Sub Procedures and Function Procedures are to be declared /defined, described/ in the source text of the program (it happens once only)

- 2. Sub Procedures and Function Procedures are to be called /activated, invoked/ (it may happen more than once)

Page 10: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

10

Sub Procedures

• General syntax to define/declare/describe

Sub ProcedureName()

block of code – VB statements

End Sub

• General syntax to call/activate/invokeCall ProcedureName()

Or justProcedureName() - Call is optional

Page 11: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

11

Module Module1

Sub Main()ShowMessage() ‘Sub invokedConsole.WriteLine(“Press Enter”)Console.ReadLine()

End Sub

Sub ShowMessage() ‘Sub definedConsole.WriteLine(“Greetings”)

End Sub

End Module

Note that user-defined procedures come after Sub Main()

Page 12: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

12

The power and versatility of a Sub may be increased by using parameters.

A parameter acts as “placeholder” for a value (of data) that you want to “pass” to the Sub.

Parameters are placed within the parentheses of the Sub declaration

Sub SubName()

Adding Parameters

Page 13: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

13

Module Module1

Sub Main()

ShowMessage(“Greetings”)ShowMessage(“Congratulations”)

Console.WriteLine(“Press Enter”)Console.ReadLine()

End Sub

Sub ShowMessage(Text As String)Console.WriteLine(Text)

End Sub

End Module

Page 14: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

14

Module Module1

Sub Main()ShowMessage(“Greetings”, 3)ShowMessage(“Congratulations”, 5)Console.WriteLine(“Press Enter”)Console.ReadLine()

End Sub

Sub ShowMessage(Text As String, Times as Integer)Dim Index As IntegerFor Index = 1 To Times

Console.WriteLine(Text)Next Index

End Sub

End Module

Page 15: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

15

Passing Data to Sub Procedure

• You can send items to a Sub procedure

Sum(2, 3)

Sub Sum(Num1 As Integer, Num2 As Integer)

• In the Sum Sub procedure, 2 will be stored in Num1 and 3 will be stored in Num2

• Num1 and Num2 are variables that are automatically available in the Sub procedure• Sum(10, 15)• Dim a As Integer = 40, b As Integer= 60• Sum(a, b)

Page 16: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

16

Parameter Passing by Value

• Keyword ByVal stands for “By Value”

• ByVal parameters retain their original value after Sub procedure terminates

Parameters must be declared using the format

ByVal Variable_Name As Data_Type

Page 17: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

17

Parameter Passing by Reference

• ByRef stands for “By Reference”

• ByRef parameters can be changed by the Sub procedure and retain the new value after the Sub procedure terminates

Parameters must be declared using the format

ByRef Variable_Name As Data_Type

Page 18: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

18

• Try to run and modify the ShowMessage()program in all the versions:

• with no parameters

• With one parameter

• With two parameters

Page 19: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

19

Functions

• Passes back a value to the calling procedure

• Calling

variable = funcname(arg1, arg2, etc)

• Function structure

Function funcname(param1 As type, param2 As type, etc) As type

statements

Return returnvalue

End Function

Returns a value

Return type

Note the assignment

Page 20: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

20

Functions

• Passes back a value to the calling procedure

• Calling

WriteLine(funcname(arg1, arg2, etc))

• Function structure

Function funcname(param1 As type, param2 As type, etc) As type

statements

Return returnvalue

End Function

Returns a value

Return type

No assignment, other context

Page 21: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

21

Example

Sub Main()Dim Item1 As Integer = 2Dim Item2 As Integer = 2Dim Sum As Integer

Sum = Add(Item1, Item2)‘Sum may be used as operand for processing‘Sum may be displayed using WriteLine()

End Sub

Function Add(Int1 As Integer, Int2 As Integer)As Integer

Return Int1 + Int2

End Function

Page 22: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

22

Exercise

• Try to run and modify the AddIntegers ()program in the following versions:

• With two parameters

• With three parameters

• With four parameters

• With five parameters

Page 23: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

23

Passing Arguments

ByVal

Sends a copy of the arguments value to a called procedure The called procedure cannot alter the original value of the arguments

ByRef

Send a reference indicating where the value is stored in memory The called procedure can alter the original value of the arguments

Page 24: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

24

Example 6-1

• Procedures as Unconditional Branching Statements– Branching to a method

Page 25: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

25

Example 6-1 Option Strict On Imports System Module Module1

Sub Main( ) Console.WriteLine("In Main! Calling SomeMethod( ) once...") SomeMethod( )

Console.WriteLine("In Main! Calling SomeMethod( ) twice...") Call SomeMethod( ) Console.WriteLine("Back in Main( ).") End Sub 'Main

Sub SomeMethod( ) Console.WriteLine(" Greetings from SomeMethod!") End Sub 'SomeMethod

End Module

Page 26: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

26

Example 6-1

Run the program.

Test the program.

Modify the program and rerun it again.

Page 27: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

27

More on unconditional branching

• Explicit statements to create unconditional branching:– Goto– Exit– Return– Throw

Page 28: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

28

Practical session 4

Procedures/Sub-s and Function-s/

Page 29: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

29

Practical task

Write a program that requests a number from 1 to 20, and then displays a row of that many asterisks using four loop constructs. Solution in three versions based on:

• VB program with Sub Main() only• User defined Sub procedure AstLine() with no

parameters• User defined Sub procedure AstLine(n) with one

parameter

Page 30: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

30

VB program with Sub Main() only

• Think your self or

• Look at the next slide

Page 31: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

31

VB program with Sub Main() onlyModule Module1

Sub Main() Dim I, n As Integer Console.WriteLine("Enter integer from 1 to 20") : n = Console.ReadLine()

Console.WriteLine() For I = 1 To n Console.Write("*") Next I

Console.WriteLine() : I = 1 While I <= n Console.Write("*") : I = I + 1 End While

Console.WriteLine() : I = 1 Do While I <= n Console.Write("*") : I = I + 1 Loop

Console.WriteLine() : I = 1 Do Console.Write("*") : I = I + 1 Loop Until I > n

Console.WriteLine() : Console.WriteLine()

End Sub

End Module

Page 32: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

32

User Sub AstLine() with no parameters

• Think your self or

• Look at the next slide

Page 33: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

33

User Sub AstLine() with no parametersModule Module1 Sub Main() AstLine() : Call AstLine() End Sub Sub AstLine() Dim I, n As Integer Console.WriteLine("Enter integer from 1 to 20") : n = Console.ReadLine()

Console.WriteLine() For I = 1 To n Console.Write("*") Next I

Console.WriteLine() : I = 1 While I <= n Console.Write("*") : I = I + 1 End While

Console.WriteLine() : I = 1 Do While I <= n Console.Write("*") : I = I + 1 Loop

Console.WriteLine() : I = 1 Do Console.Write("*") : I = I + 1 Loop Until I > n

Console.WriteLine() : Console.WriteLine()

End SubEnd Module

Page 34: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

34

User Sub AstLine(n) with one parameter

• Think your self or

• Look at the next slide

Page 35: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

35

User Sub AstLine(n) with one parameterModule Module1 Sub Main() Dim number As Integer Console.WriteLine("Enter integer from 1 to 20") : number = Console.ReadLine() AstLine(number) : Call AstLine(number) End Sub Sub AstLine(ByVal n As Integer) Dim I As Integer Console.WriteLine() For I = 1 To n Console.Write("*") Next I

Console.WriteLine() : I = 1 While I <= n Console.Write("*") : I = I + 1 End While

Console.WriteLine() : I = 1 Do While I <= n Console.Write("*") : I = I + 1 Loop

Console.WriteLine() : I = 1 Do Console.Write("*") : I = I + 1 Loop Until I > n

Console.WriteLine() : Console.WriteLine() End SubEnd Module

Page 36: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

36

Practical task

Write a program that requests a number for radius of a circle, and then displays the area of that circle. Solution in three versions based on:

• VB program with Sub Main() only• User defined Function Area() with no

parameters• User defined Function Area(n) with one

parameter

Page 37: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

37

VB program with Sub Main() only

• Think your self or

• Look at the next slide

Page 38: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

38

VB program with Sub Main() onlyModule Module1

Sub Main() Dim radius, res As Single

Console.WriteLine("Enter radius:") : radius = Console.ReadLine() res = 3.14 * radius * radius

Console.WriteLine("radius={0} area={1}", radius, res)Console.WriteLine("radius={0} area={1}", radius, 3.14 * radius * radius)

End SubEnd Module

Page 39: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

39

User Function Area() with no parameters

• Think your self or

• Look at the next slide

Page 40: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

40

User Function Area() with no parametersModule Module1

Sub Main() Dim res As Single

res = area() : Console.WriteLine("circle area={0}", res) Console.WriteLine("circle area={0}", area()) End Sub

Function area() As Single Dim r, result As Single Console.WriteLine("Enter radius:") : r = Console.ReadLine() result = 3.14 * r * r Return result

End FunctionEnd Module

Page 41: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

41

User Function Area(n) with one parameter

• Think your self or

• Look at the next slide

Page 42: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

42

User Function Area(n) with one parameter

Module Module1

Sub Main() Dim radius, res As Single Console.WriteLine("Enter radius:") : radius = Console.ReadLine() res = area(radius) : Console.WriteLine("radius={0} area={1}", radius, res) Console.WriteLine("radius={0} area={1}", radius, area(radius)) End Sub

Function area(ByVal r As Single) As Single Dim result As Single

result = 3.14 * r * r

area = result

End FunctionEnd Module

Page 43: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

43

User Function Area(n) with one parameter

Module Module1

Sub Main() Dim radius, res As Single Console.WriteLine("Enter radius:") : radius = Console.ReadLine() res = area(radius) : Console.WriteLine("radius={0} area={1}", radius, res) Console.WriteLine("radius={0} area={1}", radius, area(radius)) End Sub

Function area(ByVal r As Single) As Single Dim result As Single

result = 3.14 * r * r

Return result

End FunctionEnd Module

Page 44: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

44

User Function Area(n) with one parameter

Module Module1

Sub Main() Dim radius, res As Single Console.WriteLine("Enter radius:") : radius = Console.ReadLine() res = area(radius) : Console.WriteLine("radius={0} area={1}", radius,

res) Console.WriteLine("radius={0} area={1}", radius, area(radius)) End Sub

Function area(ByVal r As Single) As Single

return 3.14 * r * r

End Function

End Module

Page 45: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

45

User Function Area(n) with one parameter

Module Module1

Sub Main() Dim radius, res As Single Console.WriteLine("Enter radius:") : radius = Console.ReadLine() res = area(radius) : Console.WriteLine("radius={0} area={1}", radius, res) Console.WriteLine("radius={0} area={1}", radius, area(radius)) End Sub

Function area(ByVal r As Single) As Single

area = 3.14 * r * r

End FunctionEnd Module

Page 46: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

46

Practical task

Write a program that solves the Pythagorean Triple problem. Solution in three versions based on:

• VB program with Sub Main() only• User defined Sub procedure PythagorTriple1() with

two input ByVal parameters (m, n) and three output ByRef parameters (side1, side2, hypotenuse).

• User defined Function procedure PythagorTriple2() with one value returned by the function (hypotenuse), two input ByVal parameters (m, n), and two output ByRef parameters (side1, side2).

Page 47: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

47

Practical Task

Write a program that displays a n by m array (i.e. 10 rows by 10 columns) of asterisks. Solution in four versions based on:

• VB program with Sub Main() only• User defined Sub procedure Matrix()

with no parameters• User defined Sub procedure Matrix(n)

with one parameter • User defined Sub procedure Matrix(n,m) with two parameters

Page 48: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

48

Practical Task

Write a program that reads in 10 student test marks (percentages) and then displays the average mark, the lowest mark and the highest mark.

 Use these marks: 87, 56, 73, 94, 89, 82,

85, 78, 43, 89. The list is terminated by –1

Page 49: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

49

Questions?Questions?

Page 50: 1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice

50

Thank You For

Your Attention!