Qbasicnews.com

Full Version: sub or funtion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
when i do a little program for something, like draw a window of copy that to there or something like this i have to use

a SUB o a FUNTION...


is there a difference... they have diferrent speeds??? or they have a special use....
Function returns a value while sub does not. At least i think so


Function
Sub
SUBS often return values, but within specified parameter variables.

FUNCTIONS most always return a value. QB has built-in functions like SIN, LOG, SQR etc. They all return a value based on the input to the function.
Example: A = LOG(20) will return a value which is the natural log of 20, which you put into A.

You could write a function to perform some special arithmetic operation on an input value. Let's call the function SMATH. Having declared and written the function, you could now use the function as follows:
B = SMATH(256)
The function will perform the special operation on the input value 256, and return the result as a value, which you store into B.

You could write a function let's say called COMPUTECRC to compute the CRC of a string passed to the function. Having declared and written the function, somewhere in your program you could do the following:
CRC$ = COMPUTECRC$ (R$)
where R$ is the string that the CRC will be computed on. The function returns the CRC in hexadecimal notation as a string (like F1C7), which you put into CRC$. Note that this particular function did not return a value but a string.

Just for illustration, the COMPUTECRC function could have been written as a SUB, in which case you would use it as follows:
CALL COMPUTECRC (R$, CRC$)
The SUB takes the input string from the first parameter, generates the CRC, and puts the result into the string defined by the second parameter.

No big difference above between the FUNCTION and the SUB. The code itself is almost exactly the same, except that the function places the result (output) into the actual name of the function (as if it were a variable), whereas the SUB puts the output into a specified parameter.

So, you have a choice. Use FUNCTION or SUB based on which makes more sense to you at the time. There is no significant difference in speed.
*****
function returns one value and a sub can return many
GDM:
That may be your own rule for functons that you develop, but that is not true in the general case.

Although a function returns only one value for the function name, a function can pass more than one parameter to the function routine and also receive back more than one parameter, just like a SUB.
*****
Hm.... I'm just going to jump in here and say that a sub returns ZERO values and function returns ONE value. What values are changed globally is not by definition the value(s) that is "returned". (err)
Quote:Although a function returns only one value for the function name, a function can pass more than one parameter to the function routine and also receive back more than one parameter, just like a SUB.
AGA,
You will note in my above text that I distinguished between "returning a value" and "passing and receiving parameters".

In order to clear this issue up, I referred to my manual for the correct definitions and terminology:

1) A FUNCTION or a SUB are both referred to as PROCEDURES.

2) SUBS are also referred to as SUBPROGRAMS.

3) A SUBPROGRAM is a separate PROCEDURE, like a FUNCTION. However, unlike a FUNCTION, a SUB cannot be used in an expression.

4) A FUNCTION indicates that the external procedure returns a value.

5) FUNCTION names can include an explicit type character (%, &, !, #, or $) indicating the type of value the function returns.

6) "Expression" is the return value of a FUNCTION. A FUNCTION returns a value by assigning a value to the function name. If no value is assigned by the procedure, the FUNCTION returns a default value: a numeric function returns a value of zero; a string function returns a null string.

7) Both FUNCTIONS and SUBS can have an optional PARAMETERLIST.

8] A PARAMETERLIST is a comma-delimited list of variables, known as arguments, passed to the procedure when the FUNCTION or SUB is invoked. Note that these variables are passed by reference, so any change to a parameter's value inside the procedure changes its value in the calling program.

9) In summary, a FUNCTION procedure is like a SUB procedure: it can accept parameters, can perform a series of statements, has local variables, and can change the values of its parameters.

'nuf said.
*****
In addition...

In C, for example, parameters are passed by default "by value". That means that if you pass a variable "b" to a function/procedure, its value is copied in the parameter.

If QB, by default you pass variables "by reference". That means that if you pass a variable "b" to a function, the compiler puts a pointer to "b" in the stack, so if you modify that value it will remain modified outside. To avoid this, you can use "BYVAL" to make QB copy the value to a new variable instead of using the same.
This question was asked before. WIKI it?
ORA, if WIKI means put it on the FAQ, then you're right since this question keeps coming up. I don't know how to put thinks on the FAQ, so could you tell me?
*****
Pages: 1 2