| ||||||||
|
Functions and procedures are the foundations of programming. They provide the structure to organize the program into logical units that can manage the various activities needed for a program. FunctionsThere are two basic types of functions:
Functions are often treated like variables. For example:
In this example,
InStr is treated as an integer data type. Functions are typically called by other functions or procedures and given one or more variables to act upon. Types of Built-in
Functions The programming environment to accomplish activities that are often common across many applications provides built-in functions. These include:
Examples of some of these functions include: i = abs(-10) i would be equal to 10 after the function is invoked inline = input(#1, 100) inline (a string variable) would have the first 100 characters from the file referenced by a handle of #1 (the file handle is determined when the file is opened). Examples of built-in functions include the following (Note: These are functions found in Visual Basic, there are similar functions found in many languages, make sure you check the proper syntax): Math Functions: Abs return absolute
value Cos returns cosine Exp raise to exponent Int converts number to
integer Conversion Functions: Asc convert value to
ASCII format CCur convert to
currency CDate
convert to date Chr convert ASCII value
to character CInt convert to integer Format Functions Hex convert to hex
value Oct convert to octal Str convert to string Val convert to number
|
|
"functionname" must be unique. | |
|
"inputs" are optional. | |
|
"type of function" usually relates to the type of data returned when the function is called. | |
|
"actions" are one or more steps that are performed by the function | |
|
The function is set to a value prior to ending execution returning that value to the calling function |
Functions are
created for many reasons:
|
To break up a program into
more manageable segments. | |
|
To handle a repetitive task
that might be called by a number of other functions or procedures. |
Imagine a currency exchange "function" at a bank. You give the teller $10 and ask for British Pounds. The teller takes the money and returns the equivalent in Pounds.
Example:
Function
convertdollars (amount as currency, newcurrency as string) as currency
If newcurrency = "British" then
Convertdollars = amount * Britishconversionrate
Else
process other types of currency
End if
End
Function
This function would be called from somewhere else in the program as follows:
BritPounds = convertdollars($100, "British")
Procedures are used to perform a series of tasks. They usually include other procedures and functions within the program.
Procedures typically do not return a value, they are simply executed and return control to the calling procedure or subroutine.
Procedures in Visual
Basic are called "Subroutines," often "Sub" for short.
In JavaScript, "Functions" are used as procedures (they simply
return no or null values to whatever called them).
The structure for a procedure is:
Sub <subname> (<inputs>)
<actions>
End Sub
|
"subname" must be unique | |
|
"inputs" are optional; if used, they are a data type supported by the programming environment. | |
|
"actions" are one or more steps performed by the procedure. |
In environments such
as Visual Basic and Lingo (Macromedia Director) procedures can be attached to
objects and are executed when an event is triggered. For example, you might have a button called
"button1," when it is clicked, a procedure called
"button1_click" is called.
Program processes can be triggered by a command line invocation or standalone code reference.
Also used to invoke program functions passed as strings enabling end users to call program procedures or functions.
|
Copyright © 1999
- 2005 |