Subs and Functions - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Subs and Functions

Description:

1. Sub's and Functions. Using Sub's or function's to divide problems into smaller task. ... changing x is the same as changing quant. Therefore, quant =0 ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 10
Provided by: mche6
Category:
Tags: functions | quant | subs

less

Transcript and Presenter's Notes

Title: Subs and Functions


1
Subs and Functions
  • Using Subs or functions to divide problems into
    smaller task.
  • E.g. Studying High-dip can be divided into 3
    tasks -
  • STUDY_YEAR1( )
  • STUDY_YEAR2( )
  • STUDY_YEAR3( )

2
Subs
  • Subs usually performs repeating task.
  • Sample sub
  • Sub ComputeTotal ()
  • xyz
  • kkk .
  • total total kkk
  • End Sub

3
Calling the sub( )
  • To call the sub ComputeTotal,
  • Just type ComputeTotal ( )
  • Or Call ComputeTotal ( )
  • Example
  • quantity textbox1.text
  • Call ComputeTotal ( )
  • label1.text total

Sub ComputeTotal() X y Total x
y End sub
4
Passing value to subs
  • You can send values to a Sub, e.g.
    ComputeTotal (5, 6)
  • X gets the value 5 and Y gets 6
  • total becomes 30.

Sub ComputeTotal( x as integer, y as
integer) Total x y End sub
5
Passing byVal or byRef
  • byVal the sub gets the value.
  • byRef the parameter points to and uses the
    variables.
  • E.g. ComputeTotal(quant, price)
  • When passing by reference, changing x is the same
    as changing quant. Therefore, quant 0
  • and price 0 after the sub.

Sub ComputeTotal( byRef x as integer, byRef y
as integer) Total x y x 0 y 0 End sub
6
Functions
  • Same as sub, but it returns a value.
  • Example
  • Dim total as integer
  • total ComputeTotal(5,6)
  • 30 is returned to total. Note the return value is
    of type integer

Sub ComputeTotal( x as integer, y as integer)
as integer Return x y End sub
7
Build in Functions

8
Date Formats
  • When your application runs on different machine
    which has different date-time format, you need to
    do format conversion.
  • E.g. you save a date to a file
  • 29/11/2003 and your computer has a date format
    MM/dd/yyyy.
  • And you need to compare the 29/11/2003 with
    today (11/20/2003).

9
Date format conversion
  • To convert the BookDate 29/11/2003
  • to the format MM/dd/yyyy, use
  • BookDate.ToString(MM/dd/yyyy).
  • Or convert Today to the format dd/MM/yyyy as
    follows
  • todaystring Today.Date.ToString(dd/MM/yyyy)
Write a Comment
User Comments (0)
About PowerShow.com