|
Function statement |
Format:
Function name [(arglist)] [ As type ]
[ statements ]
[ Exit Function ]
[ statements ]
End Function
Syntax:
Remarks:
1. To return a value from a function, you can either assign the value to the function name or include it in a Return statement. The following example assigns the return value to the function name MyFunction and then uses the Exit Function statement to return:
Function MyFunction(A,B,C) As Double
' ...
MyFunction = 1.23
Exit Function
' ...
End Function
If you use Exit Function without assigning a value to name, the function returns the default value appropriate to type. The Return statement simultaneously assigns the return value and exits the function, as in the following example:
Function MyFunction(A,B,C) As Double
' ...
Return 1.23
' ...
End Function
Any number of Return statements can appear anywhere in the procedure.
You can also mix Exit Function and Return statements.
2. You can't define a Function procedure inside another procedure.
3. The Exit Function statement causes an immediate exit from a Function
procedure. Any number of Exit Function statements can appear anywhere
in the procedure.
4. It's possible to call other functions and subs in function procedure
but they have to be declared before current function.
5. SBasic supports recursion calls, it means you can call current Function
inside it's body.
See also:
- Recursion and functions
- Exit statement
- Return statement
- Sub statement
Example:
Function Sum(A as Floating, B as Floating, C as Floating) As Floating
Sum = A+B+C
End Function ' Returns latest value of CalcSum.
Function Mul(A,B,C) As Floating
Return A*B*C
End Function
' -----------------------------------
Input "x=",x
Input "y=",y
Input "z=",z
res = Mul(x,y,z) / Sum(x,y,z)
print "Result=", res
Copyright (c) 2000-2006 by Smartphoneware. All rights reserved.