ThePlace

Home ] Search ] Resources ] Site Map ] Contact Me ]
Dave's Information Technology Resource

Up ]

Computers & Programming ] Operating Systems ] Programming Environments ] Windows-Based Applications ] Objects and Programming ] Data and Variables ] Operational Expressions ] [ Functions and Procedures ] Programming Control ] Algorithms ] Files, Graphics & Databases ] Software Development ] 10 Things... ]

--- Functions and Procedures ---

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.

Functions

There are two basic types of functions:

bullet

Built-in—these are built into the programming environment and do things such as opening and closing files, printing, writing, and converting variables (e.g., text to numbers, singles to integers, etc.).

bullet

Application/user-specific—depending on what the program needs, you can build functions and procedures using built-in functions and procedures and variables.

 

Functions are often treated like variables.  For example:

bullet

 InStr(sourcestring, findstring) is a function in Visual Basic for locating the starting point of string (findstring) within another string (sourcestring).

bullet

The InStr function returns a number - the position of findstring.

bullet

Consider: i = InStr("Muffin is a cat.","cat")

bullet

When this function is called, "i" is set to 13

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:

bullet

String handling such as finding a string within a string.

bullet

Numeric conversion (to and from a string, between integer and long, etc.)

bullet

Financial calculations such as calculating mortgage payments.

bullet

Trigonometric calculations.

bullet

Date and time calculations.

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

 

Application-Specific Functions

Application-specific functions are written by programmers to accomplish the very specific tasks required in any given computer program.

Programmer-developed functions typically have the following structure:

Function <functionname> (<inputs>) <type of function>

  <actions>

  <functionname = value>

End Function

bullet

 "functionname" must be unique.

bullet

"inputs" are optional.

bullet

"type of function" usually relates to the type of data returned when the function is called.

bullet

"actions" are one or more steps that are performed by the function

bullet

The function is  set to a value prior to ending execution returning that value to the calling function

 

Functions are created for many reasons:

bullet

To break up a program into more manageable segments.

bullet

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

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 

bullet

"subname" must be unique

bullet

"inputs" are optional; if used, they are a data type supported by the programming environment.

bullet

"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.  

Eval Process

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.

 

Home ] Up ] Computer Architecture ] Database Bootcamp ] Visual BasicS ] Web Basics ] Web Multimedia ] Web Programming ] Advanced Web Topics ] Developing Web Sites ] XML Technology ] Web Glossary ]

Copyright © 1999 - 2005 
ThePlace - Written and Sponsored by Dave Hillman