Author: Mike Ware
Website: [warebiz] :: "The Programmer's Domain" - http://warebiz.tripod.com
Copyright © 2002 Michael Shawn Ware, All Rights Reserved.
You are here --> [Section 6 :: Using and Creating Functions
"Jump To Articles"
--> Standard / "Built-In" Functions
--> User-Defined Functions
Standard / "Built-In" Functions
By definition, a function is a subprogram designed to peform a specific task and return a value. In QBasic, there are two types of functions: standard or "built-in" and user-defined. Standard or "built-in" functions are provided by the QBasic system or compiler environment and basically allow the programmer to use them so he/she doesn't have to write code to handle certain situations. A user-defined function is written and specified by the programmer to accomplish a particular task; they always return one value to the "calling" module. Let's begin with the standard functions.
As previously mentioned, standard functions are provided by QBasic and simply have to be "called" upon for use. The actual function will have a particular name followed by parentheses. An argument is "passed" to the function by inserting a constant, variable, expression, or another function inside the parentheses. The argument is what the function will be operating on. A function has highest priority in a statement so therefore it will be evaluated before anything else in the statement. The following is a list of "built-in" numeric functions and their capablities:
ABS(number) --> Returns the absolute value of number
INT(number) --> Returns the largest integer less than or equal to number
RND --> Returns a random number between 0 and 1
SGN(number) --> Returns +1 if number is positive, 0 if number is 0, and -1 if number is negative
SQR(number) --> Returns the square root of a positive number
NOTE: In the above examples, the variable number is being "passed" as an argument.
NOTE: RND is used to produce a random number from 0 to 1, but you can also restrict it to produce random numbers in different ranges. When using RND, you will also need to place RANDOMIZE TIMER somewhere in the program before you try to produce a random number using RND. The RANDOMIZE TIMER statement is used to get a new seed for the random numbers. The "seed" is determined by the computer's internal clock. If you are going to use random numbers in your program, you should typically place RANDOMIZE TIMER at the beginning of your program; it only needs to be placed one time in the program. To produce a number in a specified range, use the following RND format:
randomNum = RND * ( NUM_OF_NUMS_IN_RANGE + 1 ) + LOWERBOUND
where ( NUM_OF_NUMS_IN_RANGE + 1 ) is the total number of numbers in the range you want to restrict the RND function to and LOWERBOUND is the low limit for the range. You can make the function produce only integers by using the INT( ). For example, if we want to produce a random number ( integer ) in the range -15 to 45, we could use:
randomNum = INT( RND * 61 - 15 )
Enough with numeric functions, the following is list of handy string functions and their capabilities:
string1 + string2 --> joints (concatenates) two (or multiple) strings
ASC(string) --> Returns the ASCII value for the first character in string
CHR$(expression) --> Evaluates the ASCII value of expression and returns the equivalent string value
DATE$ --> Returns the current data determined by computer's internal clock
LCASE$(string) --> Converts any uppercase letters in string to lowercase
UCASE$(string) --> Converts any lowercase letters in string to uppercase
LEN(string) --> Returns the number of characters contained in string
STR$(expression) --> Converts a number (expression) into a string
STRING$(expression, character) --> Generates character a specified number of times determined by expression
VAL(string) --> Returns the numeric value of a numeric string
LEFT$(string, expression) --> Returns the number of leftmost characters in string specified by expression
MID$(string, expression1, expression2) --> Starting at the character specified by expression1, it returns the number of characters in string specified by expression2
RIGHT$(string, expression) --> Returns the number of rightmost characters in string specified by expression
The following is a complete program that demonstrates most of the numeric and string functions described above:
REM THIS PROGRAM DEMONSTRATES THE USE OF "BUILT-IN" NUMERIC
REM AND STRING FUNCTIONS
REM PROGRAMMER: MIKE WARE
REM DATE LAST UPDATED: 4-17-01
INPUT "Enter a positive or negative number: ", number
INPUT "Enter your first name: ", firstName$
INPUT "Enter your last name: ", lastName$
PRINT "YOUR INPUT"
PRINT " number = "; number
PRINT " First Name: "; firstName$
PRINT " Last Name: "; lastName$
PRINT "The absolute value of number is: "; ABS(number)
PRINT "The largest integer less than or equal to number is: "; INT(number)
IF SGN(number) = 1 THEN
PRINT "number is positive."
PRINT "The square root of number is: "; SQR(number)
ELSEIF SGN(number) = -1 THEN
PRINT "number is negative."
PRINT "number is neutral (0)."
ranNum = INT(RND * (number + 1) + 0)
PRINT "A randon integer from 0 to "; number; "is: "; ranNum
PRINT "Full Name (nospaces): "; firstName$ + lastName$
PRINT "Full Name (lowercase/nospaces): "; LCASE$(firstName$ + lastName$)
PRINT "Full Name (uppercase/nospaces): "; UCASE$(firstName$ + lastName$)
PRINT "The length of your first name is: "; LEN(firstName$)
REM demonstrates use of data validation
INPUT "Are you married (y/n): "; married$
married$ = LEFT$(UCASE$(married$), 1)
IF married$ = "Y" THEN
PRINT "Congratulations, "; firstName$; " "; lastName$
PRINT firstName$; " "; lastName$; ", your love will come soon."
While standard functions are pre-defined and provided for by QBasic, user-defined functions are completely defined and customized by the programmer. User-defined functions return a single value and are generally used to perform an operation that will be needed numerous times in a program. In QBasic, user-defined functions are referred to as procedures; similar to SUB procedures except function procedures return one value [see Using Modules (section 4)]. Arguments may be sent into a function procedure for use in the function operation, but the value returned by the function will not be included in the parameter list. The value is returned in the function itself. The form of a function procedure is as follows:
FUNCTION name( parameter list )
REM body of function
Like SUB procedures, you can create a FUNCTION by typing FUNCTION and the name you wish to give it in the compiler window and a seperate window will be displayed for you to create the function. You can then use the F2 key to "toggle" among functions and modules. You can use the function by "calling" it the program where you need to use it by simply using the name of the function and sending arguments, if any, to the paramter list.
The following complete program illustrates the use of a user-defined function (function procedure):
DECLARE FUNCTION getGrade$ (score1!, score2!, score3!, score4!, score5!)
REM THIS PROGRAM USES A USER-DEFINED FUNCTION (FUNCTION PROCEDURE)
REM TO CALCULATE A STUDENT'S GRADE BASED ON FIVE SCORES WITH
REM SCORE EACH HAVING A DIFFERENT WEIGHTED VALUE TOWARD THE
REM STUDENT'S OVERALL AVERAGE
REM PROGRAMMER: MIKE WARE
REM DATE LAST UPDATED: 4-11-01
INPUT "Enter the student's name: ", stuName$
INPUT "Enter the student's five test scores (seperated by a comma): ", score1, score2, score3, score4, score5
grade$ = getGrade$(score1, score2, score3, score4, score5)
PRINT stuName$; ", earned a grade of ("; grade$; ")"
FUNCTION getGrade$ (score1, score2, score3, score4, score5)
quizzes = (score1 + score2 + score3) / 300 * 100 * .4
tests = (score4 + score5) / 200 * 100 * .6
totalScore = quizzes + tests
SELECT CASE totalScore
CASE IS > 90
getGrade$ = "A"
CASE IS > 80
getGrade$ = "B"
CASE IS > 70
getGrade$ = "C"
CASE IS > 60
getGrade$ = "D"
getGrade$ = "F"
In the next section, you will be introduced to how to use arrays to store related data values and also how to sort array elements in order and search arrays for particular data. Read on for more...
Move on to next set of topics: Section 7 - Arrays
Back to Top