110 likes | 255 Views
CS105 Discussion 7 – Subprocedures, Functions, Debugging. Announcements If you did not get 50/50 on MP1, you have until 9pm on October 12 th to submit again. If your score on Compass is zero and the checker gave you a better score, please contact me before you leave today. Objectives.
E N D
CS105 Discussion 7 – Subprocedures, Functions, Debugging • Announcements • If you did not get 50/50 on MP1, you have until 9pm on October 12th to submit again. • If your score on Compass is zero and the checker gave you a better score, please contact me before you leave today. Fall 2009
Objectives • Learn to write simple functions • Learn to call functions and sub-procedures • Learn to use the debugger to understand control flow and step through your program Fall 2009
Task 1: The intSquare function • Our first task is to write a simple function that computes the square of a given integer • Function takes a single argument intX and returns an int Private Function intSquare(intX As Integer) As Integer intSquare = intX * intX End Function • The function name (intSquare) is a variable that holds the final answer (the return value of the function). Fall 2009
Task 2: Calling the intSquare function • So far, we have merely defined the intSquare function • It is a tool we created, but we haven’t actually used it yet • When the Add button is clicked, we want to add 22 to cell A1 • We will use intSquare(2) to compute 22 Private Sub cmdAdd_Click() [A1] = [A1] + intSquare(2) End Sub Fall 2009
Task 3: Stepping through the code • We will now step through the program starting at the point when the Add button is clicked • Click on the line “Private Sub cmdAdd_Click()” and press F8 • Press F8 once, and the yellow bar jumps to the next line • Press F8 once more, and it jumps to the intSquare function • We have just called the intSquare function with argument intX = 2 • Hover the mouse over variable intX to see its current value Fall 2009
Stepping through the code (contd.) • Press F8 twice more until you reach see this: • The function intSquare is about to return the value 4 (22 = 4) • Press F8 once more to see what happens to this value: • The cell A1 (which started off with value 0) now has value 4 • Thus, the function call intSquare(2) produced the return value 4: [A1] = [A1] + intSquare(2) • When you call a function, you give it data (arguments), and you use the answer (return value) add it to A1 in this case. Fall 2009
Task 4: Complex function arguments • The argument given to intSquare does not have to be a fixed number like 2, 3, etc. • It can be a variable, like in this example: • It can be a more complex expression, like this: [A1] = [A1] + intSquare(intX + 1) • It can even be the return value of a function! Examples: [A1] = [A1] + intSquare(intSquare(2)) [A1] = [A1] + intSquare(intCube(intX)) Step through these to make sure the order of computation makes sense! Fall 2009
Task 5: Adding a simple If-Else • Let’s go back to this case: [A1] = [A1] + intSquare(2) • Click on the Reset button to set A1 to 0 • Each time Add is clicked, A1’s value increases by 4 • Suppose we want to skip over the value 12, and reset A1 to 0 when it reaches 32 [A1] = [A1] + intSquare(2) If [A1] = 12 Then [A1] = [A1] + intSquare(2) ElseIf [A1] = 32 Then [A1] = 0 End If Step through this to make sure the control flow makes sense! Fall 2009
Task 6: Self-clicking the Add button • Instead of manually re-clicking the Add button, wouldn’t it be nice if we could automatically re-click it? • Let’s add one more line of code to do this: • What is this last line before End Sub doing? • We’re calling the subroutine cmdAdd_Click • This has the effect of re-clicking the button Run this code and see what happens! Fall 2009
Debugging • If your program tries to do something illegal, you see an error message: • Click on Debug to see where the error was This particular message says that the poor computer ran out of space! Without “stack space”, functions/subs cannot be called! If an error occurs within a function (as here), the debugger stops where the function was called, NOT where the error actually occurred! Fall 2009
Setting Breakpoints • To pause the program at a particular line, you can click on the gray border to the left of that line: • At this line, we have a breakpoint • Click on Add to run the code. You'll see that the computer stops running at the breakpoint. • Press F8 to keep step through the program • Press F5 to run the program normally (until the breakpoint is encountered again). To remove the breakpoint, just click on the circle. Fall 2009