Procedures PowerPoint PPT Presentation

presentation player overlay
1 / 25
About This Presentation
Transcript and Presenter's Notes

Title: Procedures


1
Procedures Functions
  • Chapter 4
  • CS0004
  • http//www.cs.pitt.edu/bmills/cs4/

2
Administration
  • Part 1 of Project 1 is due
  • Will grade and return Wednesday
  • Review on Wednesday and present good designs
  • Instructions for turning in project
  • Try to have this to you by Wednesday
  • Questions?

3
Review
  • Math operators
  • Strings and their functions
  • Declaring Variables and Data Types
  • Decisions
  • If blocks
  • Select Case Blocks

4
Recall
  • Already seen procedure and functions?
  • Event Procedure
  • Called when an event occurs, weve been doing
    this for a while
  • Built-in Functions
  • Math.Sqrt(number)
  • SubString(index, length)

5
Sub-Procedures
  • Event Procedures
  • Private Sub btnAdd_Click() Handles btnAdd.Click
  • --- Do Stuff In Here ---
  • End Sub
  • General Procedures - User Defined
  • Sub MySubProcedure()
  • --- I do MY stuff here ---
  • End Sub

6
Why Use Procedures?
  • Break difficult tasks into smaller pieces
  • Example
  • Need to convert meters into yards
  • Display instructions to the user
  • Open a file
  • Save a file to a location

7
Parameters
  • These are variables that you pass into the
    procedure or function.
  • Recall all that junk in the Event Procedure
  • Private Sub btn_Click(ByVal sender As
    System.Object,
  • ByVal e As System.EventArgs)
  • Handles btn.Click
  • sender and e are parameters to btn_Click

8
Parameters
  • Lets create a sub procedure to display the
    average given the sum and count
  • Sub DisplayAvg(ByVal sum As Double, ByVal count
    As Double)
  • txtOutput.Text sum / count
  • End Sub

9
Parameters
  • Lets create a sub procedure to display the
    average given the sum and count
  • Sub DisplayAvg(ByVal sum As Double, ByVal count
    As Double)
  • txtOutput.Text sum / count
  • End Sub

count is also a parameter name
sum is the parameter name
10
Parameters
  • Lets create a sub procedure to display the
    average given the sum and count
  • Sub DisplayAvg(ByVal sum As Double, ByVal count
    As Double)
  • txtOutput.Text sum / count
  • End Sub

count is also of data type Double
sum is of data type Double
11
Parameters
  • Lets create a sub procedure to display the
    average given the sum and count
  • Sub DisplayAvg(ByVal sum As Double, ByVal count
    As Double)
  • txtOutput.Text sum / count
  • End Sub

Notice that inside the sub-procedure we can use
these variables.
Parameters are treated like variable
declarations inside the sub-procedure
12
Using a Procedure
  • You Call these procedures
  • Sub DisplayAvg(ByVal sum As Double, ByVal count
    As Double)
  • txtOutput.Text sum / count
  • End Sub
  • Sub btnAvg_Click() Handles btnAvg.Click
  • Dim the_sum, the_count As Double
  • the_sum CDbl(txtSum.Text)
  • the_count CDbl(txtCount.Text)
  • DisplayAvg(the_sum, the_count)
  • End Sub

This is called a procedure call
13
Variable Scope
  • When can you access what variables?
  • Sub MyProcedure(ByVal num As Integer)
  • Dim x as Double
  • x num 20.5
  • txtOutput x
  • End Sub
  • Sub MyOtherProc(ByVal num As Integer)
  • Dim x as Integer
  • x num 1
  • MyProcedure(x)
  • End Sub

14
Variable Scope
  • When can you access what variables?
  • Sub MyProcedure(ByVal num As Integer)
  • Dim x as Double
  • x num 20.5
  • txtOutput x
  • End Sub
  • Sub MyOtherProc(ByVal num As Integer)
  • Dim x as Integer
  • x num 1
  • MyProcedure(x)
  • End Sub

These are the same name but in different scopes
15
Variable Scope
  • When can you access what variables?
  • Sub MyProcedure(ByVal num As Integer)
  • Dim x as Double
  • x num 20.5
  • txtOutput x
  • End Sub
  • Sub MyOtherProc(ByVal num As Integer)
  • Dim x as Integer
  • x num 1
  • MyProcedure(x)
  • End Sub

These too
16
Variable Scope
  • When you use a variable the computer looks for
    where it is declared
  • Starts close to home and expands search
  • Close to Home Local Scope
  • Farther from Home Class-level Scope
  • As soon as it finds the variable declaration it
    uses that one

17
Class Variables
  • Visible to every procedure
  • Declared after the Public Class Form1 but
    before any procedures
  • Limit the number of class variables used try to
    stick to local variables
  • Example Time!

18
ByVal and ByRef
  • ByVal tells the computer to copy the Value of the
    passed in data into the local scope
  • The scope of the parameter is said to be local
  • Like variables declared inside this procedure,
    this parameter will cease to exist when the
    procedure ends
  • The value of the argument passed to this
    procedure is copied into the parameter
  • The value of the argument in the calling
    procedure is UNCHANGED when control returns to
    the caller

19
ByVal and ByRef
  • ByRef tells the computer to NOT copy the data but
    LINK them
  • Changes made to the parameter in the called
    procedure are reflected in the calling procedure
  • THIS IS DANGEROUS
  • Try to avoid this if at all possible
  • If you want a value returned, use a Function
  • If you want a variable visible to all procedures,
    use a Class Variable

20
Functions
  • Sub Procedures do not return any values
  • Many times, this return value is desired
  • Calculating an average
  • Convert meters into yards
  • Convert Fahrenheit into Celsius
  • This is the reason we have functions

21
Functions
  • Much like Sub-procedures
  • Function FuncName(parameters) As Double
  • Dim x As Double
  • x 2.5
  • Return x
  • End Function
  • Note that here we need to declare what type of
    value the Function will return
  • The value we want passed is done so using the
    Return statement

22
Functions
  • Notice that functions return values so you can
    use them just like other expression
  • Function foo() As String
  • Return This is my output
  • End Function
  • Sub btnClick(..) Handles btn.Click
  • txtOutput.Text foo()
  • End Sub

23
Functions Sub-Procedures
  • Functions used where wed find expressions
  • Subroutines used where wed find chunks of code
  • Both perform similar tasks
  • Both can call each other from other Subroutines
    or Functions

24
Modular Design
  • Save on typing
  • Break big tasks into small procedures and
    functions
  • Keep from making errors (easy to test)
  • What are some examples in your project?

25
Lets try it out
  • Design a program to convert Fahrenheit to Celsius
  • Use a function to compute the conversion
  • Use a sub procedure to display the result
Write a Comment
User Comments (0)
About PowerShow.com