1 / 97

INF110 Visual Basic Programming AUBG Spring semester 2011

irish
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 Fall semester 2009Reference 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 10 Title: Visual Basic (Procedures1: Sub-s & Function-s)

  3. Lecture Contents: • Introducing the procedures concept • Advantages of using procedures • Sub procedure • Function procedure • Data exchange btw procedures • Parameter passing mechanism • Actual arguments • Formal parameters

  4. INF110 Visual Basic Programming Procedures (Sub-s and Function-s)

  5. The problem to be solved: To compute a Pythagorean triple using two integers m and n (m>n) as input and applying following formulas to compute all the three sides of a right triangle: side1 = m2–n2 side2 = 2*m*n hypotenuse = m2+n2 Problem specification: Input: Two integers: m, n Output: Three values: side1, side2, hypotenuse Process: Formulas provided

  6. Introduction to Sub Procedures The problem to be solved: To compute 3 Pythagorean triples using two integers m and n (m>n) as input and applying following formulas to compute all the three sides of a right triangle: side1 = m2–n2 side2 = 2*m*n hypotenuse = m2+n2 Problem specification: Input: Two integers: m, n Output: Three values: side1, side2, hypotenuse Process: Formulas provided

  7. Introduction to Sub Procedures • The problem to be solved: • To compute 3 Pythagorean triples using two integers m and n (m>n) as input and applying following formulas to compute all the three sides of a right triangle: • side1 = m2–n2 side2 = 2*m*n hypotenuse = m2+n2 • How to solve the problem? There exist many (at least 3) different approaches: • Duplicating 3 times solving code – bad solution • Using repetition control structure – better solution • Using a procedure as a separate independent program unit – the best solution

  8. Duplicating code solution Module Module1 Sub Main() Dim m, n As Integer Dim Side1, Side2, Hypotenuse As Integer Console.WriteLine("Enter 2 numbers m,n (m>n):") m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) Side1 = m*m - n*n Side2 = 2*m*n Hypotenuse = m*m + n*n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) End Sub End Module

  9. Duplicating code solution Source code in red has to be duplicated 3 times in order to solve the problem Module Module1 Sub Main() Dim m, n As Integer Dim Side1, Side2, Hypotenuse As Integer Console.WriteLine("Enter 2 numbers m,n (m>n):") m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) Side1 = m*m - n*n Side2 = 2*m*n Hypotenuse = m*m + n*n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) End Sub End Module

  10. Duplicating code solution Source code after being duplicated 3 times in order to solve the problem Module Module1 Sub Main() Dim m, n As Integer Dim Side1, Side2, Hypotenuse As Integer Console.WriteLine("Enter 2 numbers m,n (m>n):") m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) Side1 = m*m - n*n Side2 = 2*m*n Hypotenuse = m*m + n*n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) Console.WriteLine("Enter 2 numbers m,n (m>n):") m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) Side1 = m*m - n*n Side2 = 2*m*n Hypotenuse = m*m + n*n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) Console.WriteLine("Enter 2 numbers m,n (m>n):") m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) Side1 = m*m - n*n Side2 = 2*m*n Hypotenuse = m*m + n*n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) End Sub End Module

  11. Using repetition control structure – better solution The user or the developer may be asked to compute 5, 10, 25 or more Pythagorean triples Obviously, the solution must not be based on duplicatingcode We can use the repetition control structure(s) like For … Next Do While (condition) … Loop Do … Loop Until (condition) And other modifications of loop statements from VB

  12. Using repetition control structure – better solution Module Module1 Sub Main() Dim m, n, counter As Integer Dim Side1, Side2, Hypotenuse As Integer For counter = 1 To 3 Step 1 Console.WriteLine("Enter 2 numbers m,n (m>n):") m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) Side1 = m * m - n * n Side2 = 2 * m * n Hypotenuse = m * m + n * n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) Next counter End Sub End Module

  13. Using repetition control structure – better solution Module Module1 Sub Main() Dim m, n, counter As Integer Dim Side1, Side2, Hypotenuse As Integer counter = 1 Do While counter <= 3 Console.WriteLine("Enter 2 numbers m,n (m>n):") m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) Side1 = m * m - n * n Side2 = 2 * m * n Hypotenuse = m * m + n * n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) counter = counter + 1 Loop End Sub End Module

  14. Using repetition control structure – better solution Module Module1 Sub Main() Dim m, n, counter As Integer Dim Side1, Side2, Hypotenuse As Integer counter = 1 Do Console.WriteLine("Enter 2 numbers m,n (m>n):") m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) Side1 = m * m - n * n Side2 = 2 * m * n Hypotenuse = m * m + n * n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) counter = counter + 1 Loop Until counter = 3 End Sub End Module

  15. Using a procedure – best solution The solution based on loop statements is better than the duplicating code but it keeps the same skeleton/structure of the console application: Module Module1 Sub Main() . . . End Sub End Module A better, more structured solution may be implemented using another approach established as a corner stone in structured programming – all the activities to solve a specific problem to be concentrated/encapsulated in a separate independent program unit - procedure. VB supports two types of separate program units – subroutines Sub and functions Function.

  16. Using a procedure – best solution Applying this structured approach means a new skeleton/structure of the console application: The new procedure has to be defined/declared Module Module1 Sub Main() . . . End Sub Sub PythagorTriple() . . . End Sub End Module

  17. Using a procedure – best solution Applying this structured approach means a new skeleton/structure of the console application: The new procedure has to be defined/declared Module Module1 Sub Main() . . . End Sub Sub PythagorTriple() Dim m, n As Integer Dim Side1, Side2, Hypotenuse As Integer Console.WriteLine("Enter 2 numbers m,n (m>n):") m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) Side1 = m * m - n * n Side2 = 2 * m * n Hypotenuse = m * m + n * n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) End Sub End Module

  18. Using a procedure – best solution After the procedure being specified, it may be called, activated, invoked 3, 5, 10, 15, 25, … as many times as the user needs using a calling statement typed in the caller /master/ unit. The procedure itself is called /slave/ unit. Module Module1 Sub Main() . . . PythagorTriple() Call PythagorTriple() PythagorTriple() . . . End Sub Sub PythagorTriple() . . . End Sub End Module

  19. Using a procedure – best solution After the procedure being specified, it may be called, activated, invoked 3, 5, 10, 15, 25, … as many times as the user needs using a statement typed in the calling /master/ unit. The procedure itself is called /slave/ unit. Module Module1 Sub Main() . . . PythagorTriple() : Call PythagorTriple() PythagorTriple() : PythagorTriple() : PythagorTriple() . . . End Sub Sub PythagorTriple() . . . End Sub End Module

  20. Using a procedure – best solution After the procedure being specified, it may be called, activated, invoked 3, 5, 10, 15, 25, … as many times as the user needs using a statement typed in the calling /master/ unit. The procedure itself is called /slave/ unit. Module Module1 Sub Main() . . . PythagorTriple() . . . End Sub Sub PythagorTriple() . . . End Sub End Module

  21. Using a procedure – best solution After the procedure being specified, it may be called, activated, invoked 3, 5, 10, 15, 25, … as many times as the user needs using a statement typed in the calling /master/ unit. The procedure itself is called /slave/ unit. Module Module1 Sub Main() . . . Dim counter As Integer = 1 Do While counter <= 10 PythagorTriple() counter = counter + 1 Loop . . . End Sub Sub PythagorTriple() . . . End Sub End Module

  22. Using a procedure – best solution Entire program source text Module Module1 Sub Main() PythagorTriple() Call PythagorTriple() End Sub Sub PythagorTriple() Dim m, n As Integer Dim Side1, Side2, Hypotenuse As Integer Console.WriteLine("Enter 2 numbers m,n (m>n):") m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) Side1 = m * m - n * n Side2 = 2 * m * n Hypotenuse = m * m + n * n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) End Sub End Module

  23. Using a procedure – best solution All I-P-O encapsulated inside Sub PythagorTriple() Dim m, n As Integer Dim Side1, Side2, Hypotenuse As Integer Console.WriteLine("Enter 2 numbers m,n (m>n):") m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) Side1 = m * m - n * n Side2 = 2 * m * n Hypotenuse = m * m + n * n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) End Sub The PythagorTriple() Sub procedure illustrates one primitive and restricted (but not the only possible) approach of solving a problem when all the I-P-O activities are encapsulated within the procedure body: Input Data for m,n are typed (entered) at run time from the keyboard. Side1, Side2, Hypotenuse are evaluated through assignment statements. Output (result) data displayed on screen through Console.WriteLine().

  24. Using a procedure – best solution Data exchange btw procedures Introduction to parameter passing mechanism • Ways to assign data for m,n: • Initialization • Dim mAs Integer = 8, n As Integer = 6 • Assignment • Dim m,n As Integer = 6 • m = 8 : n = 6 • Typing /reading/ data from the keyboard or from input file • Console.WriteLine("Enter 2 numbers m,n (m>n):") • m = CInt(Console.ReadLine()) • n = CInt(Console.ReadLine()) • Data exchange btw procedures: Caller/Master procedure sends data and Called/Slave procedure receives data sent by Master through parameter passing mechanism. • See next slide for details.

  25. Using a procedure – best solution Data exchange btw procedures Introduction to parameter passing mechanism • Data exchange btw procedures: Caller/Master procedure sends data and Called/Slave procedure receives data sent by Master through parameter passing mechanism. • Data sent by Master is known as actual data and appears in the calling statement as a list of actual arguments separated by comma. • Data received by Slave to be processed appears in the header/title of the called procedure as a list of formal parameters separated by comma. • The correspondence btw actual arguments and formal parameters by number and data type is must.

  26. Using a procedure – best solution Data exchange btw procedures Introduction to parameter passing mechanism 2. Data sent by Master is known as actual data and appears in the calling statement as a list of actual arguments separated by comma. The calling statement PythagorTriple() Is to be replaced with PythagorTriple(8, 6) or Dim Arg1 As Integer = 8, Arg2 As Integer = 6 PythagorTriple(Arg1, Arg2) or Dim Arg1, Arg2 As Integer Arg1 = 10 : Arg2 = 6 PythagorTriple(Arg1+4*Arg2, Arg2-6 mod 3 *2)

  27. Using a procedure – best solution Data exchange btw procedures Introduction to parameter passing mechanism • Data received by Slave to be processed appears in the header/title of the called procedure as a list of formal parameters separated by comma. • The non-parameter procedure body • Sub PythagorTriple() • Dim m, n As Integer • Dim Side1, Side2, Hypotenuse As Integer • Console.WriteLine("Enter 2 numbers m,n (m>n):") • m = CInt(Console.ReadLine()) : n = CInt(Console.ReadLine()) • Side1 = m * m - n * n • Side2 = 2 * m * n • Hypotenuse = m * m + n * n • Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) • End Sub • Is to be replaced with

  28. Using a procedure – best solution Data exchange btw procedures Introduction to parameter passing mechanism Sub PythagorTriple(m As Integer, n As Integer) Dim Side1, Side2, Hypotenuse As Integer Side1 = m * m - n * n Side2 = 2 * m * n Hypotenuse = m * m + n * n Console.WriteLine("Result is {0} {1} {2}", Side1, Side2, Hypotenuse) End Sub

  29. Devices for modularity • VB.NET has two constructs for dividing problems into smaller pieces (sub-problems) • Sub procedures • Function procedures • Some people call them methods They are blocks of code that can be referred to by a name. - Rather like having a program within a program

  30. Sub Procedures and Function Procedures - Allow removal of duplicated code within the same program – just write block of code once and refer to it from anywhere in the program - Leads to better structured programming - Well-designed procedures and functions can be reused in other applications • - Needs to be stand-alone code • - The only link to the rest of the program is through parameters – data sent to the block of code. • - Needs to have a specific functionalitye.g. calculating some quantity

  31. SubProcedures and Function Procedures - Two activities are must for effective processing with SubProcedures and Function Procedures in VB: - 1. SubProcedures and Function Procedures are to be declared or defined or described in the source text of the program (it happens once only) - 2. SubProcedures and Function Procedures are to be called or activated (it may happen more than once)

  32. Sub-procedures (or just Subs) and Functions (or more precisely Function procedures) may be invoked (or called) any number of times.

  33. The two coming slides present whatB.Kernighan and D.Ritchie think on functions in CIt’s absolutely valid for both types of procedures (Sub and Function) in Visual Basic

  34. Basics of procedures: “Properly designed functions permit to ignore how a job’s done. Knowing what is done is sufficient.” B.Kernighan & D.Ritchie Most important reasons to use procedures: Dividing a program into procedures is one of the major principles of structured programming. Using procedures results in reduced program size.

  35. Basics of procedures: “A function provides a convenient way to encapsulate some computation, which can then be used without worrying about its implementation. ” B.Kernighan & D.Ritchie Usually procedures are specified to be called many times. You can see often a short procedure defined and called only once, just because it clarifies some piece of code.

  36. Sub Procedures • Performs one or more related tasks • General syntax • Sub ProcedureName() • block of code – VB statements • End Sub

  37. Sub Procedure Structure • Sub SubName() • declarations • statements • End Sub sub-procedure name local declarations – “private” to the sub procedure Statements perform the “function” of the sub procedure

  38. Naming Sub procedures • The rules for naming Sub procedures are the same as the rules for naming variables.

  39. Example Sub Squares10()     Dim Num As Integer    Dim NumSquared As Integer    For Num = 1 To 10        NumSquared = Num*Num        Console.Writeline(NumSquared)    Next Num End Sub The statements to display the squares of the first ten numbers written as a Sub procedure

  40. Calling a Sub Procedure In order to invoke a sub procedure, i.e. to get its statements executed, the sub procedure must be called from wherever in the program its functionality is needed. When a sub procedure is called, the current sequence of statement execution is temporally suspended, and the statements of the sub procedure are executed instead. When the sub procedure statements are completed, a return is made to the previous sequence of statement execution. To invoke or call a sub procedure, its name must be written, e.g. ProcedureName()

  41. Calling a Sub procedure • The statement that invokes a Sub procedure is also referred to as a call statement • A call statement looks like this • Call ProcedureName() • Or just • ProcedureName() - Call is optional • Calling a sub procedure is effectively an unconditional branch in the code.

  42. A sub procedure is called by naming it in a statement. Note the brackets Example ... Gaga() CallSub Gaga ... Sub Gaga() ... Sub procedure code ... End Sub Return from Sub Gaga - a return is made to the statement following the Call

  43. Module Module1 Sub Main() End Sub End Module You may not be aware of it, but we have been using the Sub procedure Main in our programs already.

  44. Example Console.Writeline(variable) The system Sub procedure Console.Writeline() displays the value of a variable.

  45. Module Module1 Sub Main() ShowMessage() Console.WriteLine(“Press Enter”) Console.ReadLine() End Sub Sub ShowMessage() Console.WriteLine(“Greetings”) End Sub End Module Note that user-defined procedures come after Sub Main()

  46. It is possible for one sub procedure to call another, and so on. Effectively, we have a number of nested Sub calls.

  47. Sub Procedures Calling Other Sub Procedures Sub Gaga() ... FirstPart() End Sub Sub FirstPart() SecondPart() End Sub Sub SecondPart() ... End Sub

  48. Adding Parameters Data exchange among procedures Calling or Caller or Master procedure And Called or Slave procedure The Master procedure sends data to Slave procedure via actual arguments. The Slave procedure receives data coming from Master via formal parameters. The actual_arguments/formal_parameters correspondence

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

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

More Related