CS105 Lab Discussion 10 Functions and Subprocedures - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CS105 Lab Discussion 10 Functions and Subprocedures

Description:

If you have a conflict with the exam time, sign up for the conflict exam as soon ... Based on when it is available and how much the total rent is, we want to tell ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 18
Provided by: cs101
Category:

less

Transcript and Presenter's Notes

Title: CS105 Lab Discussion 10 Functions and Subprocedures


1
CS105 Lab Discussion 10Functions and
Subprocedures
  • Announcements
  • MP4 is posted and is due on Saturday, October
    28th at 1130am.
  • Midterm 2 will take place on October 30th. If you
    have a conflict with the exam time, sign up for
    the conflict exam as soon as possible.
  • The review sessions will be held in 141 Wohlers
    on Sunday, October 29th at 3pm and 7pm. The
    reviews will last until your questions run out,
    or for a maximum of two hours.

2
Objectives
  • Review If statements
  • Write your own functions and subprocedures
  • Go to the course website and download the extra
    material for Lab 10.
  • http//www.cs.uiuc.edu/class/cs105

3
The Plan
  • In this lab, we have some information about an
    apartment sublet opening. Based on when it is
    available and how much the total rent is, we
    want to tell the user if he/she should rent the
    apartment or not.
  • The Rent? Button will determine if the user
    should rent the apartment and store the results
    on the worksheet.
  • The Clear Button will clear the results.

4
Figuring out the total rent
  • Right now we are only given how much the rent is
    per month. We need to figure out how much the
    total rent is going to be. We will write a
    function called TotalRent to do this.
  • We have already provided you with the skeleton
    code. To view it, go to the two drop-down boxes
    on the top of the code window and select
    (General) and TotalRent

5
Declaring TotalRent()
  • The TotalRent() function takes in the rent per
    month as a parameter and returns the total rent
    for one term. To declare this function
  • Function TotalRent(curRent As Currency) _ As
    Currency

Function Name
Parameters
Return Type
6
Returning a Value
  • Remember A function always returns a value!
  • How do we tell our function to return a value?
  • We write
  • ltFunction namegt ltValuegt
  • When execution reaches the end of the function,
    it returns the last value assigned to the
    function name.

7
TotalRent() (cont'd)
  • We want TotalRent() to return the total rent for
    one term. The number of months in one term is 3.
  • TotalRent curRent 3

8
Calling TotalRent()
  • Now lets call TotalRent and store the return
    value in C10. Remember that TotalRent takes the
    rent per month as an argument. We get this from
    cell D5.
  • Look back at the function prototype to see how we
    use this function.
  • Function TotalRent(curRent As Currency) As
    Currency
  • Cells(10, 3).Value TotalRent(Cells(5, 4).Value)

9
Declaring vs. Calling a Function
  • When we declare a function, we are telling VBA
    what kinds of arguments it takes, as well as what
    it will return.
  • The names of the arguments in the declaration are
    what the function use to reference the
    information inside the function.
  • When we call a function, we are actually using
    the function.
  • The arguments we pass to the function dont have
    to have the same names as the names of the
    arguments inside the function. They do have to
    have the same types, however.

10
Error Checking
  • We also need to know the term the apartment is
    available. Terms must be either "Fall",
    "Winter", "Spring", or "Summer".
  • Let's add some error-checking to make sure the
    term is valid. To do this, we will define a
    function called IsValidTerm()

11
Declaring IsValidTerm()
  • This function takes the term as an argument and
    returns True if the term is valid otherwise it
    returns False. Since it returns True or False,
    the return type is Boolean.
  • Function IsValidTerm(vntTerm As Variant) _ As
    Boolean

12
IsValidTerm
  • We want to return True if the term equals
    "Summer", "Winter", "Fall", or "Spring". We can
    do this using the following syntax for the If
    statement
  • If vntTerm Summer _
  • Or vntTerm Winter _
  • Or vntTerm Fall _
  • Or vntTerm Spring Then
  • IsValidTerm True
  • Else
  • IsValidTerm False
  • End If
  • Whats another way we could write this If
    statement using multiple Else Ifs?

13
Calling IsValidTerm()
  • Now go back to cmdRent and use IsValidTerm()
  • If Not IsValidTerm(Cells(5, 3).Value) Then
  • MsgBox "Term is not valid"
  • Exit Sub
  • End If

14
Do we want to rent it?
  • The user wants to know if he/she wants to rent
    the apartment. The user has two conditions
  • The total rent must be under 1000
  • The apartment must be available in the summer

15
Displaying the results
  • We will define a subprocedure DisplayAnswer that
    will display the results in C11C13. Below is the
    subprocedure declaration. Since subprocedures
    don't return a value, we don't specify a return
    type.
  • Sub DisplayAnswer(curRent As _ Currency, vntTerm
    As Variant)

Note that this Subprocedure takes two arguments
Subprocedure name
16
DisplayAnswer
  • We want to fill in the answers in C11C13.
  • If the total rent is under 1000, then store
    "Yes" in C11. Otherwise store "No" in C11.
  • If the term is "Summer", then store "Yes" in C12.
    Otherwise store "No" in C12.
  • Finally, if C11 and C12 both contain "Yes", then
    store "Yes" in C13. Otherwise store "No" in C13.

17
Calling DisplayAnswer
  • Go back to the code for cmdRent
  • Call DisplayAnswer by adding the following line
    of code
  • DisplayAnswer Cells(10,3).Value, _
    Cells(5,3).Value
  • Note the difference in the way the subprocedure
    is called from how a function is called
Write a Comment
User Comments (0)
About PowerShow.com