English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

In Visual Basic. What is the difference between the formula and a function??

2007-02-27 10:53:55 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

A function is a type of subroutine which returns a value which is usually assigned to a variable but not alway. For example a function can be used in a IF statement and its results used to determine how the IF statment will branch the program (True or False)
IF myFunc THEN

or an assignment to a variable

y = myFunc(x)

A formula, for example a conversion from Farenheit to Celcius, does not make a subroutine call .

C = (F-32)/1.8

this formular is executed on one line of code without branching program flow and making a subroutine call.


This same formula can easily be converted to a function

public function FtoC( F as double) as double
dim C as double
C= (F-32)/1.8
return C
end function

your program would then use the function

C = FtoC( 212 )

This usage makes a subroutine call to the function. Program flow is temporarily halted. Program step count is pushed onto a stack the function is called passing the data, the function now executes the code and returns the answer. The program step count is poped from the stack and the program continues running.

A simple function has a little more processing overhead than a formula. Unless you are making a large nuber of function calls any delay is not noticible. Usually the benefits of enhanced readabliity and code encapsulation far out weigh any overhead penality a function call has.

2007-02-27 12:15:51 · answer #1 · answered by MarkG 7 · 0 0

They're quite simmilar, actually. A formula is a calculation

=SUM(a1+a2+a3)

and a FUNCTION is any piece of code that returns a value...A FORMULA is one type of FUNCTION.

2007-02-27 18:58:33 · answer #2 · answered by Richard H 7 · 0 0

fedest.com, questions and answers