11
1 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. Fall 2009

CS105 Discussion 7 – Subprocedures, Functions, Debugging

  • Upload
    harris

  • View
    26

  • Download
    1

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: CS105 Discussion 7 – Subprocedures, Functions, Debugging

1

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

Page 2: CS105 Discussion 7 – Subprocedures, Functions, Debugging

2

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

Page 3: CS105 Discussion 7 – Subprocedures, Functions, Debugging

Fall 2009 3

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

Given integer intX intSquare(intX)

1 1

2 4

-1 1

Page 4: CS105 Discussion 7 – Subprocedures, Functions, Debugging

Fall 2009 4

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

Page 5: CS105 Discussion 7 – Subprocedures, Functions, Debugging

Fall 2009 5

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

Page 6: CS105 Discussion 7 – Subprocedures, Functions, Debugging

Fall 2009 6

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.

Page 7: CS105 Discussion 7 – Subprocedures, Functions, Debugging

Fall 2009 7

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 throughthese to makesure the orderof computationmakes sense!

Page 8: CS105 Discussion 7 – Subprocedures, Functions, Debugging

Fall 2009 8

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 thisto make sure thecontrol flowmakes sense!

Page 9: CS105 Discussion 7 – Subprocedures, Functions, Debugging

Fall 2009 9

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 andsee what happens!

Page 10: CS105 Discussion 7 – Subprocedures, Functions, Debugging

Fall 2009 10

Debugging

If your program tries to do something illegal, you see an error message:

Click on Debug to see where the error was

This particularmessage saysthat the poorcomputer ranout of space!

If an error occurs withina function (as here), thedebugger stops wherethe function was called,NOT where the erroractually occurred!

Without “stack space”,functions/subs cannotbe called!

Page 11: CS105 Discussion 7 – Subprocedures, Functions, Debugging

Fall 2009 11

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.