Beginner's Programming Tutorial in QBasic

(Kiana) #1

' the subroutine's parameters.
SUB OutputNumber (num)
PRINT num
END SUB
Output:
16


TIP: Variables created in your program cannot be
used in the subroutines unless you use COMMON
SHARED (followed by a variable) in the main
module.
COMMON SHARED x$

Functions


A function is the same as a subroutine, except it returns a value. Also, you must leave out the
CALL command.
To return a value, set a variable with the same name as the function.
PRINT Add(10, 7)
FUNCTION Add (num1, num2)
Add = num1 + num2
END FUNCTION
Output:
17


Since a function can return a value, the name of the function can end with special characters (see
Variable types, Using special characters).
' Notice the dollar sign ($) after "Add." It means' the function returns a string.
PRINT Add$("Hello", "World")
FUNCTION Add$ (str1$, str2$)
Add$ = str1$ + str2$

Free download pdf