INFS308 Spring 2006 - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

INFS308 Spring 2006

Description:

Pass by Value/Ref. Arguments passed by reference (default) ... Pass by Value/Ref. Arguments passed by value. Let curRate = 35.00 Let curHrs = 10.00 ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 27
Provided by: BenM1
Category:
Tags: infs308 | pass | spring

less

Transcript and Presenter's Notes

Title: INFS308 Spring 2006


1
INFS308Spring 2006
  • University of Colorado
  • At
  • Colorado Springs
  • Ben Martz

2
Overview
  • Subroutines
  • Procedures Functions
  • Scope
  • Parameters
  • Conversions
  • Implicit vs Explicit
  • Widening vs Narrowing

3
Procedure Functions
  • What is basic difference between function and
    procedure?
  • What are benefits of breaking code down?
  • Procedures Functions
  • require a name
  • have a scope (private vs. public)
  • may have arguments/parameters

4
Procedure Example
  • Assume that your program repeated the same
    statements over and over in order to calculate
    the amount earned by an employee. You can create
    a procedure to do those two statements and
    replace each of your two statements with one.
  • Private Sub CalculatePay()
  • curTotalPay curHrlyRate intHrsWorked
  • MsgBox Total Pay is , curTotalPay
  • End Sub
  • Now a single line CalculatePay will perform the
    calculation.

5
Example
1.) Trace the logic through to its end 2.) What
is displayed
  • Private Sub cmdTestMultipleCalls_Click()
  • DIM X as Integer
  • X100
  • TestProcedureA
  • TestProcedureB
  • MsgBox cmdTestMultipleCalls_Click X X
  • End Sub
  • Private Sub TestProcedureA()
  • DIM X as Integer
  • X 200
  • TestProcedureB
  • MsgBox TestProcedureA X X
  • End Sub
  • Private Sub TestProcedureB()
  • DIM X as Integer
  • X 300
  • MsgBox TestProcedureB X X
  • End Sub

6
Example (revised)
1.) Trace the logic through to its end 2.) What
is displayed
  • Private Sub cmdTestMultipleCalls_Click()
  • DIM X as Integer
  • X100
  • TestProcedureA
  • TestProcedureB
  • MsgBox cmdTestMultipleCalls_Click X X
  • End Sub
  • Private Sub TestProcedureA()
  • DIM X as Integer
  • X 200
  • TestProcedureB
  • MsgBox TestProcedureA X X
  • End Sub
  • Private Sub TestProcedureB()
  • DIM X as Integer
  • TestProcedureA()
  • X 300
  • MsgBox TestProcedureB X X
  • End Sub

7
Function Example
  • Assuming the same details as before except we
    want the amount returned for further processing,
    a function would work better.
  • Establish function with Private Function
  • Private Function curPay (curHrlyRate as Currency,
    _
  • curHours
    as Currency) as Currency
  • Let curPay curHrlyRate curHours
  • End Function
  • Use function as regular function call
  • Let curRate val(txtHourlyRate.txt)
  • Let curHrs val(txtCurHours.txt)
  • Let curMyPay curPay(curRate, curHrs)

8
Function example
  • Arguments MUST match in number, order, type
  • Number 2 order is logical types match
  • Let curRate val(txtHourlyRate.txt)
  • Let curHrs val(txtCurHours.txt)
  • Let curMyPay curPay(curRate, curHrs)
  • Private Function curPay(curHrlyRate as Currency,
    curHours as Currency) as Currency
  • Let curPay curHrlyRate curHours
  • End Function

9
Function example
  • Arguments MUST match in number, order, type
  • Number 2 order is logical types match
  • Let curRate val(txtHourlyRate.txt)
  • Let curHrs val(txtCurHours.txt)
  • Let curMyPay curPay(curRate, curHrs)
  • Private Function curPay(curHrlyRate as Currency,
    curHours as Currency) as Currency
  • Let curPay curHrlyRate curHours
  • End Function

Rate is first hours is second in both cases
10
Function example
  • Arguments MUST match in number, order, type
  • Number 2 order is logical types match
  • Let curRate val(txtHourlyRate.txt)
  • Let curHrs val(txtCurHours.txt)
  • Let curMyPay curPay(curRate, curHrs)
  • Private Function curPay(curHrlyRate as Currency,
    curHours as Currency) as Currency
  • Let curPay curHrlyRate curHours
  • End Function

Must assign a value for function to
return. Assigned to the function name!!!
11
Pass by Value/Ref
  • Arguments passed by reference (default)
  • Let curRate 35.00 Let curHrs 10.00
  • Let curMyPay curPay(curRate, curHrs)
  • Private Function curPay(curHrlyRate as Currency,
    curHours as Currency) as Currency
  • Let curPay curHrlyRate curHours
  • Let curHours 11.00
  • End Function

curRate 35.00
curHrs 10.00
What impact does this statement have on curHrs?
12
Pass by Value/Ref
curRate 35.00
  • Arguments passed by value
  • Let curRate 35.00 Let curHrs 10.00
  • Let curMyPay curPay(curRate, curHrs)
  • Private Function curPay(by Val curHrlyRate as
    Currency, _
  • by Val
    curHours as Currency) as Currency
  • Let curPay curHrlyRate curHours
  • Let curHours 11.00
  • End Function

curHrs 10.00
curHrlyRate 35.00
curHours 10.00
What impact does this statement have on curHrs?
13
Pass by Value/Ref
curRate 35.00
  • Arguments passed by value
  • Let curRate 35.00 Let curHrs 10.00
  • Let curMyPay curPay(curRate, curHrs)
  • Private Function curPay(by Val curHrlyRate as
    Currency, _
  • by Val
    curHours as Currency) as Currency
  • Let curPay curHrlyRate curHours
  • Let curHours 11.00
  • End Function

curHrs 10.00
curHrlyRate 35.00
Why is the difference between value and reference
important? Reference is for real its valuable
to make a copy
curHours 10.00
14
Code Modules
  • Remember the idea is to create reusable code.
    Procedures and Functions are examples. Two more
    examples in VB are
  • Code Modules
  • Modules of code that is reusable
  • SUB MAIN
  • Predefined procedure that executes when program
    starts
  • Libraries
  • Presorted functions procedures
  • Generally added to programs to gain functionality

15
Procedures Have Scope Too
  • Private Sub
  • Available to the form
  • Public Sub
  • Available to the program
  • Must identify with
  • form.procedurename
  • frmEx1.CalculatePay
  • Code Module
  • Available across program
  • Can reference with procedure name as code in Code
    Module is by default, public (uses Public Sub
    declaration).

16
Conversions
  • Data Types have an order smaller to larger.
  • Data can move from small to large types without
    losing precision this is not true for data
    moving from larger to smaller types
  • Why care?
  • Implicit is done automatically (Option Strict)
  • Single 14.54 to integer 15
  • Integer 14 to single 14.00
  • Explicit is done programatically
  • Mydollar Convert.ToString(intdollaramount)

17
Summary
  • Procedures Functions
  • Procedures have scope too
  • know difference(s) between functions and
    procedures
  • know apply rules for arguments/parameters
  • know difference between pass by value pass by
    reference
  • Coversions
  • Why do we care?
  • Implicit vs Explicit
  • Widening vs Narrowing

18
Practice
  • The next ten slides take you through some
    practice with understanding the pass by value
    and the pass by reference concepts. See if you
    can do them w/o looking at the solutions in the
    notes.

19
Ref/Val Practice
Dim mintA, mintB, mintC, mintD as Integer mintA
4 mintB 5 mintC 2 mintD 3 Call
scramble ltltlt no parameters passed gtgtgt
Scramble
Dim mintA, mintB, mintC, mintD as Integer mintA
mintD mintB mintC
What are values mintA mintB mintC mintD
20
Ref/Val Practice
Dim mintA, mintB, mintC, mintD as Integer mintA
4 mintB 5 mintC 2 mintD 3 Call
scramble (mintA, mintB, mintC, mintD)
Scramble (intA, intB, intC, intD as Integer)
intA intD intB intC
What are values mintA mintB mintC mintD
21
Ref/Val Practice
Dim mintA, mintB, mintC, mintD as Integer mintA
4 mintB 5 mintC 2 mintD 3 Call
scramble (mintA, mintB)
Scramble (by val intA, intB as integer)
intA mintD intB mintC
What are values mintA mintB mintC mintD
What are values mintA mintB mintC mintD intA
intB
22
Ref/Val Practice
Dim mintA, mintB, mintC, mintD as Integer mintA
4 mintB 5 mintC 2 mintD 3 Call
scramble (mintA, mintB)
Scramble (by ref intA, intB as integer)
intA mintD intB mintC
What are values mintA mintB mintC mintD
What are values mintA mintB mintC mintD intA
intB
23
Larger
  • You have been asked to write a function to 1)
    accept two integer numbers as arguments 2)
    determine which of the two numbers is larger and
    3) pass the larger of the two numbers back as the
    result.
  • Try to create this

24
Largest
  • You have been asked to write a function to 1)
    accept three integer numbers as arguments 2)
    determine which of the three numbers is larger
    and 3) pass the larger of the three numbers back
    as the result.

25
Odd or Even
  • Create a function to determine whether or not an
    integer is odd or even. The function should
    accept a single integer and return a value of
    true or false.

26
Parsing a Filename
  • Write a function that accepts a fully
    instantiated filename and returns the filename
    only.
  • Example c\misa\presentation.ppt
  • returns presentation.ppt
  • Now add a set of option buttons to have the user
    choose filename, subdirectory or root directory.
    Write a function for each choice.
Write a Comment
User Comments (0)
About PowerShow.com