BA394 VBScript Lecture 4 - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

BA394 VBScript Lecture 4

Description:

Sub DoMultiply(Value1, Value2) In the previous example, Value1 and Value2 can be anything that the caller wants ... function DoMultiply(Value1, Value2) with the ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 14
Provided by: busine
Category:

less

Transcript and Presenter's Notes

Title: BA394 VBScript Lecture 4


1
BA394 VBScript Lecture 4
  • Tim Faltemier

2
In this Lecture
  • Look into the use of Subroutines and Functions
    for cleaner code.
  • Session Variables
  • Look at any issues with your projects that will
    be due next week.

3
Sub Routines (Procedures)
  • The purpose of Sub routines is to allow the
    programmer to reuse code throughout his or her
    program.
  • A Sub routine can be seen in many forms. The
    most common of which would probably be the
    Response.write() sub.
  • To understand a little more about the Sub
    routine, you must first know about arguments and
    returns.

4
Arguments
  • An Argument is simply something in the sub code
    that allows another particular sub or function to
    get data from another location (usually outside
    the sub)
  • A typical Sub would look like the following
  • Sub DoMultiply(Value1, Value2)
  • In the previous example, Value1 and Value2 can be
    anything that the caller wants them to be (1,2)
    etc..

5
Returns
  • Returns work in a very similar way to a sub but
    in the opposite direction. A Return is something
    that your FUNCTION will send back to the program
    when it is done.
  • For example If you call the function
    DoMultiply(Value1, Value2) with the values (2, 4)
    it would Return 8 to the location that called it.

6
Sub Routines (II)
  • Now that you understand arguments and returns, we
    can get a little further. Sub routines have NO
    return value. Whatever they do / change, they do
    it INSIDE its code.
  • So in order to use these effectively, you should
    either call them to change something on the
    screen (write different values etc..) or to
    change Global Variables.
  • You can see the example syntax of a Sub on the
    next slide.

7
Sub Routine Syntax
  • The syntax is as follows (without the lt and gt)
  • Sub ltMySubNamegt (ltArg1gt, Arg2, etc..)
  • ltsome code goes heregt
  • ltsome more code goes heregt
  • ltall the code you wantgt
  • End Sub
  • Note you can have as many arguments as you want!

8
Functions
  • Now that you hopefully understand Sub routines,
    Functions will be simple.
  • The only thing that separates Functions and Subs
    is the fact that Functions return a value to the
    user.
  • Most of the time is is better to use Functions
    rather than subs because you can always use the
    value that it returns.

9
Functions (II)
  • In the worse case scenario (you can not think of
    something to return) return either true or false
    to tell you if the function worked correctly.
  • Doing this will save you HUGE amounts of time
    during debugging.
  • You can see the syntax of a Function on the next
    page (Note it looks JUST like a Sub but watch
    the detail on the bottom of the page.)

10
Functions (III)
  • Note that the function definition is usually All
    on the same line.
  • Function ltMySubNamegt (ltArg1gt, Arg2, etc..)
  • ltsome code goes heregt
  • ltsome more code goes heregt
  • ltMySubNamegt True or False or equal to any
    variable that you want it to be that you want to
    return.
  • End Function

11
Session Variables
  • If you have ever programmed before and know what
    the concept of global variables are, then you
    know what session variables are.
  • Session variables are no different from normal
    variables in most aspects, the only difference is
    that they are stored as long as the user is on
    your site or has the browser open.
  • Session variables come in the form
  • Session(ltMyVARgt) ltMyValuegt

12
Questions or Comments
13
Final Notes
  • For the next quiz, make sure you look at and
    understand all of the examples from my site and
    read the lecture notes.
  • Also make SURE you check back on the website
    before the quiz next week as there may be changes
    noted etc..
  • Finally if you have any other questions or
    problems dont hesitate to email me.
Write a Comment
User Comments (0)
About PowerShow.com