Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS0004: Introduction to Programming Subprocedures and Modular Design.

Similar presentations


Presentation on theme: "CS0004: Introduction to Programming Subprocedures and Modular Design."— Presentation transcript:

1 CS0004: Introduction to Programming Subprocedures and Modular Design

2 Review  The difference between arguments and parameters is…  Arguments are the values you pass to a function  Parameters are the local variables used in the function header.  General form of a function: Function FunctionName (ByVal var1 As Type, ByVal var2 As Type) As ReturnDataType … some code … Return expression End Function

3 Review  A function definition is…  The code for the function (or the header and body)  A function header is…  From the word Function to the return type  A function body is…  Between the function header and the words End Function. In other words the code that is executed when it is called.  Scope is…  Where a variable can be used (seen).

4 Subprocedures  Subprocedures are functions that DO NOT return a value.  They are used to break down your code into logical sections (modularize).  General Form: Sub SubprocedureName (ByVal var1 As Type, ByVal var2 As Type) … some code … End Sub  Notice that there is no return type or return statement  All the rules that apply to functions apply to sub procedures EXCEPT that you cannot (therefore you do not have to) return a value

5 Arguments and Parameters  How are arguments are parameters related?  Subprocedure Definition Sub mySub(ByVal a As String, ByVal b As String) MessageBox.show(a,b) End Sub  Subprocedure Call mySub(“Eric”, “Who is the best?”)  What does this function call do?  It displays a message box with the message “Eric” and the title “Who is the best”  Why?  Because the parameter a gets the value “Eric” from the argument in the function call and the parameter b gets the value “Who is the best?” from the other argument in the call.

6 Subprocedure Example  New Topics:  Subprocedures

7 Procedures Calling Other Procedures  Subprocedures or functions can call other subprocedures or functions, even themselves. Sub mySub1() line of code 1 mySub2() line of code 3 End Sub Sub mySub2() line of code 4 line of code 5 End Sub

8 Passing by Value vs. Passing by Reference  Remember the ByVal keyword in front of the parameters?  This keyword indicates that all arguments are passed to this parameter by value.  Passing by value says that a variable argument passed to a function will always retain its value at the time it was passed. Dim a As Integer = 24 mySub(a) MessageBox.show(a) Sub mySub(ByVal inA As Integer) inA = 999 End Sub  Literals, expressions, and variables can be passed by value

9 Passing by Value vs. Passing by Reference  In addition to ByVal there is also ByRef  This keyword indicates that all arguments are passed to this parameter by reference.  Passing by reference says that a variable argument passed to a function will retain the value that the FUNCTION SETS THE CORRESPONDING PARAMETER.  In other words, whatever happens to the parameter, happens to the argument. Dim a As Integer = 24 mySub(a) MessageBox.show(a) Sub mySub(ByRef inA As Integer) inA = 999 End Sub  Only variables can be passed by reference

10 Pass by Value/Pass by Reference Example  New Topics:  Passing by Value  Passing by Reference

11 Pass By Reference Notes  If you do not need an argument to take a value from what is done in the procedure, then pass it by value, otherwise you may have unintended consequences happen.  If you only have one parameter that needs to take a value from what is done in the procedure, it is good programming practice to simply make the procedure a function procedure and return that value.  Passing by value can be seen as being able to return multiple values from a procedure.

12 A Few More Notes on Scope…  If a variable or constant is declared in a procedure, its lifetime is from the time it is declared to the end of the procedure.  Lifetime of a variable is the period where it still resides in memory.  A variable or constant (or a parameter, really) declared in a procedure is called a local variable or local constant (local to the procedure), and is said to have local scope.  A variable not declared in a procedure, but in a class definition is said to have class-level scope.  A variable declared in an if statement, select case statement, or a loop (next chapters subject) is said to have block-level scope. Also, its lifetime is untill the block (if, select case, etc.) ends  Local scope – can be referenced anywhere in the procedure in which it was declared in  Class-level scope – can be referenced anywhere in the class (including its procedures) in which it was declared in  Block-level scope – can be referenced anywhere in the block in which it was declared.

13 Modular Design  Many programs can be broken down into smaller sub- tasks.  Remember the Hierarchy Charts…

14 Hierarchy Charts Postage Stamp Program Read SheetsCalculate StampsDisplay Stamps Get sheets Make sure sheets is a positive integer. Display: "You will need: " # of stamps Set stamps = sheets / 5 Round stamps up to next whole number Must be number Must be positive Must be integer

15 Modular Design  Here we took a problem and kept breaking it down into smaller and smaller parts. This is called stepwise refinement.  Stepwise refinement is a part of a design methodology called top-down design.  Top-down design general tasks occur near the top of the design and tasks representing their refinement occur below  When using top-down design, the goal is to break down a problem into individual tasks called modules. Thus, when problem is broken down into smaller subtasks, the design is said to be modular.  Modular design is simply a way to design a solution. The implementation of that design is where it appears in the code.

16 Modular Design  If you implement a program from a modular design, your program should then have the following characteristics:  It is easy to write  It makes a problem easy to conceptualize  If you write modular procedures, it is possible that you may need the same code elsewhere. It is then said this code is reusable.  It is easy to debug  After you write a modular procedure, you can then test it by itself for bugs. When it is bug free, you do not have to worry about it adversely affecting other modules with bugs it may have.  It is easy to understand  It is easy to change  If a specific part of a program needs to be changed, simply go to that procedure and change it

17 Recursion  Recursion (in computer science) is a method where the solution to a problem depends on solutions to smaller instances of the same problem.  This is often implemented as a function calling itself in programming.  Without a base-case, recursion would go on endlessly.  A base-case is the situation where a recursive function returns a result NOT dependent on the result from another recursive call.  This happens naturally often in math  Factorial  Fibonacci Numbers  Ackermann Function

18 Recursion Example 1 Function Factorial(ByVal n As Integer) As Integer If n > 1 Then Return n * Factorial(n-1) Else Return 1 End If End Function

19 Recursion Example 2  Fibonacci in VB


Download ppt "CS0004: Introduction to Programming Subprocedures and Modular Design."

Similar presentations


Ads by Google