1 / 50

INF110 Visual Basic Programming AUBG Spring semester 2011

clint
Download Presentation

INF110 Visual Basic Programming AUBG Spring semester 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. INF110 Visual Basic Programming AUBG Spring semester 2011 Lecture 11 Title: Visual Basic (Procedures2: Sub-s & Function-s)

  3. Lecture Contents: • Reminder – basic Concepts • ByVal parameters • ByRef parameters • Demo programs • Practical session

  4. INF110 Visual Basic Programming Procedures Demo programs by J.Liberty, Chapter 06 0601

  5. Reminder

  6. 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

  7. 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.

  8. 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)

  9. 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

  10. 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()

  11. 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()

  12. 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

  13. 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

  14. 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)

  15. 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

  16. 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

  17. Try to run and modify the ShowMessage()program in all the versions: • with no parameters • With one parameter • With two parameters

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. Example 6-1 • Procedures as Unconditional Branching Statements • Branching to a method

  24. 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

  25. Example 6-1 Run the program. Test the program. Modify the program and rerun it again.

  26. More on unconditional branching • Explicit statements to create unconditional branching: • Goto • Exit • Return • Throw

  27. Practical session 4Procedures/Sub-s and Function-s/

  28. 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

  29. VB program with Sub Main() only • Think your self or • Look at the next slide

  30. 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

  31. User Sub AstLine() with no parameters • Think your self or • Look at the next slide

  32. 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

  33. User Sub AstLine(n) with one parameter • Think your self or • Look at the next slide

  34. 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

  35. 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

  36. VB program with Sub Main() only • Think your self or • Look at the next slide

  37. 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

  38. User Function Area() with no parameters • Think your self or • Look at the next slide

  39. 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

  40. User Function Area(n) with one parameter • Think your self or • Look at the next slide

  41. 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

  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 Return result End Function End Module

  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 return 3.14 * r * r End Function End Module

  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 area = 3.14 * r * r End Function End Module

  45. 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).

  46. 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

  47. 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

  48. Questions?

  49. Thank You For Your Attention!

More Related