Download presentation
Presentation is loading. Please wait.
Published byPatrick Chandler Modified over 9 years ago
1
1 INF110 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
2
2 INF110 Visual Basic Programming AUBG Spring semester 2011 Lecture 11 Title: Visual Basic (Procedures2: Sub-s & Function-s)
3
3 Lecture Contents: §Reminder – basic Concepts §ByVal parameters §ByRef parameters §Demo programs §Practical session
4
4
5
5 Procedures Demo programs by J.Liberty, Chapter 06 0601 INF110 Visual Basic Programming
6
6 Reminder
7
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
8
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.
9
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)
10
10 Sub Procedures General syntax to define/declare/describe Sub ProcedureName() block of code – VB statements End Sub General syntax to call/activate/invoke Call ProcedureName() Or just ProcedureName() - Call is optional
11
11 Module Module1 Sub Main() ShowMessage()‘Sub invoked Console.WriteLine(“Press Enter”) Console.ReadLine() End Sub Sub ShowMessage() ‘Sub defined Console.WriteLine(“Greetings”) End Sub End Module Note that user-defined procedures come after Sub Main()
12
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
13
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
14
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 Integer For Index = 1 To Times Console.WriteLine(Text) Next Index End Sub End Module
15
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)
16
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
17
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
18
18 Try to run and modify the ShowMessage() program in all the versions: with no parameters With one parameter With two parameters
19
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
20
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
21
21 Example Sub Main() Dim Item1 As Integer = 2 Dim Item2 As Integer = 2 Dim 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
22
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
23
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
24
24 Example 6-1 Procedures as Unconditional Branching Statements –Branching to a method
25
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
26
26 Example 6-1 Run the program. Test the program. Modify the program and rerun it again.
27
27 More on unconditional branching Explicit statements to create unconditional branching: –Goto –Exit –Return –Throw
28
28 Practical session 4 Procedures /Sub-s and Function-s/
29
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
30
30 VB program with Sub Main() only Think your self or Look at the next slide
31
31 VB program with Sub Main() only Module 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
32
32 User Sub AstLine() with no parameters Think your self or Look at the next slide
33
33 User Sub AstLine() with no parameters Module 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 Sub End Module
34
34 User Sub AstLine(n) with one parameter Think your self or Look at the next slide
35
35 User Sub AstLine(n) with one parameter Module 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 Sub End Module
36
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
37
37 VB program with Sub Main() only Think your self or Look at the next slide
38
38 VB program with Sub Main() only Module 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 Sub End Module
39
39 User Function Area() with no parameters Think your self or Look at the next slide
40
40 User Function Area() with no parameters Module 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 Function End Module
41
41 User Function Area(n) with one parameter Think your self or Look at the next slide
42
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 Function End Module
43
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 Function End Module
44
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
45
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 Function End Module
46
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).
47
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
48
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
49
49 Questions?
50
50 Thank You For Your Attention!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.