E N D
INF110Visual Basic ProgrammingAUBG Spring semester 2011Reference 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 libraryCourse lecturer: Assoc. Prof. Svetla Boytcheva, PhD
INF110 Visual Basic Programming AUBG Spring semester 2011 Lecture 11 Title: Visual Basic (Procedures2: Sub-s & Function-s)
Lecture Contents: • Reminder – basic Concepts • ByVal parameters • ByRef parameters • Demo programs • Practical session
INF110 Visual Basic Programming Procedures Demo programs by J.Liberty, Chapter 06 0601
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
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.
SubProcedures and Function Procedures - Two activities are must for effective processing with Sub Procedures and Function Procedures in VB: - 1. SubProcedures and Function Procedures are to be declared /defined, described/ in the source text of the program (it happens once only) - 2. SubProcedures and Function Procedures are to be called /activated, invoked/ (it may happen more than once)
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
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()
Adding Parameters 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()
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
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
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)
Parameter Passing by Value • Keyword ByVal stands for “By Value” • ByVal parametersretain their original value afterSub procedureterminates Parameters must be declared using the format ByValVariable_Name As Data_Type
Parameter Passing by Reference • ByRef stands for “By Reference” • ByRef parameterscan be changed bythe Sub procedureand retain the new value afterthe Sub procedureterminates Parameters must be declared using the format ByRefVariable_Name As Data_Type
Try to run and modify the ShowMessage()program in all the versions: • with no parameters • With one parameter • With two parameters
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 Note the assignment Return type Returns a value
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 No assignment, other context Return type Returns a value
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
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
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
Example 6-1 • Procedures as Unconditional Branching Statements • Branching to a method
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
Example 6-1 Run the program. Test the program. Modify the program and rerun it again.
More on unconditional branching • Explicit statements to create unconditional branching: • Goto • Exit • Return • Throw
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
VB program with Sub Main() only • Think your self or • Look at the next slide
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
User Sub AstLine() with no parameters • Think your self or • Look at the next slide
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
User Sub AstLine(n) with one parameter • Think your self or • Look at the next slide
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
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
VB program with Sub Main() only • Think your self or • Look at the next slide
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
User Function Area() with no parameters • Think your self or • Look at the next slide
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
User Function Area(n) with one parameter • Think your self or • Look at the next slide
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
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
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
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
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).
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
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
Thank You For Your Attention!