QBASIC subroutines
Results 1 to 6 of 6

Thread: QBASIC subroutines

  1. #1
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351

    Post QBASIC subroutines

    I am in a QBASIC course at school. It's very simple, but it has helped with other languages. Upon coming to the conclusion that my teacher is incompetent, I wrote a tutorial for the current chapter we're on. It just explains the basics of subroutines because they were all having trouble with the difference between parameters, arguments, and variables.

    Here you are:

    Consider the following program:

    Code:
    DECLARE SUB add (a, b)
    
    CLS
    x = 4
    y = 3
    PRINT “Calling subroutine Add”
    
    CALL add(x, y)
    
    SUB useless
    ‘this SUB will never be executed
    PRINT “You will not see this text on screen.”
    END SUB
    
    SUB add (a, b)
    z = a + b
    PRINT a; "+"; b; "="; z
    END SUB
    
    END


    Line-by-line explaination:

    -DECLARE is a non-executable statement that declares references to QBASIC
    procedures and ensures argument type checking. This means that you cannot pass more than two arguments to add, and they must be integers.

    -CLS is the first executable statement. DECLARE must always precede executable statements.

    -x = 4 and y = 3 are actually LET statements. LET takes two arguments, a variable and an expression. “x” or “y” are the variables, and the expression is whatever follows the “=” sign.

    -CALL is required to enter a subroutine. If call was not there, then the program would simply end. CALL requires that you specify the name of the subroutine you are calling. QBASIC then searches for the corresponding SUB statement. If there are any parameters defined in the DECLARE statement then QBASIC checks the arguments against the parameters to be sure that type and amount of each match. This means you cannot pass the string “Hello” to an integer variable ‘x’.

    -The SUB statement defines the beginning of a subroutine. SUB is a command taking one argument, the function and it’s corresponding parameters.

    -END SUB defines the end of a subroutine and returns program control to the statement following “CALL ” in the calling subroutine/function.

    -Once again SUB is encountered and QBASIC is aware that the statements from SUB to END SUB will not be executed (unless called by a CALL statement).

    - As you can see, the variables x and y are passed to add. x and y are the arguments. They are “passed by reference” to add’s parameters a and b. Within the subroutine, a, b, and z are all local variables. x and y are global variables, the scope of these variables is global, or they can be read anywhere in the program. a, b, and z have scopes that are local to the subroutine add. If you were to have the statement:
    [FONT=courier
    new]PRINT z[/FONT]
    Following the CALL statement, you would see a 0 printed to the screen, because z is not recognized by the main portion of the program.

    NOTES: If you want to make a variable available to all subroutines/functions in the program, precede it with SHARED . You must initialize the variable as SHARED before assigning a value.

    Example:


    Code:
    SHARED var
    var = 5


    To allow a variable to be used in any module (other files in the same program) or another program, they must be declared as COMMON . Consult QBASIC’s help feature for more information on the COMMON keyword.

    In contrast, the keyword SHARED can be used to make a variable local to a specific module and preserve its value between calls.
    Geek isn't just a four-letter word; it's a six-figure income.

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    Not bad, I hope it helped out your classmates. I think I'll stick with other languages though . Hope your classes go well.

  3. #3
    Leftie Linux Lover the_JinX's Avatar
    Join Date
    Nov 2001
    Location
    Beverwijk Netherlands
    Posts
    2,535
    w0w they still teach QUICKBASIC ??

    /me gets all nostalgic..

    oh.. the times.. the sweet sweet times..
    ASCII stupid question, get a stupid ANSI.
    When in Russia, pet a PETSCII.

    Get your ass over to SLAYRadio the best station for C64 Remixes !

  4. #4
    AFLAAACKKK!!
    Join Date
    Apr 2004
    Posts
    1,065
    Yes, I took that course in school as well... A little to boring for me though...

    Anyway, good tutorial.
    I am the uber duck!!1
    Proxy Tools

  5. #5
    In And Above Man Black Cluster's Avatar
    Join Date
    Feb 2005
    Posts
    912
    Nice tutorial,

    Actually, i am taking up the same course. It is just in time tutorial.
    \"The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards - and even then I have my doubts\".....Spaf
    Everytime I learn a new thing, I discover how ignorant I am.- ... Black Cluster

  6. #6
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351
    I'm glad others actually found it useful. QBASIC is a quite useful language in many aspects...and especially good for beginners. It gave me a nice jumpstart on C++.

    A_T
    Geek isn't just a four-letter word; it's a six-figure income.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 Security News

     Patches

       Security Trends

         How-To

           Buying Guides