Repository navigation
procedures Function
Home › Procedures › Function
Defines a procedure returning a value
[Public|Private] Function identifier [cdecl|pascal|stdcall] [Overload] [Alias external_identifier] [([parameter_list])] [ ByRef ] As return_type [Static] [Export]
statements
...
{ {Return [return_value]}|{Function = return_value}|{identifier = return_value} }
...
End Functionidentifierexternal_identifierparameter_listparameter[, parameter[, ...]] parameteridentifiertypedefault_valuedefault_valuereturn_typestatementsreturn_value
A function defines a block of code which can be executed with a single statement (a function call), and provide a value back to the caller when finished (a return value). There are several reasons to use functions:
- Reduces redundancy in your program
- Enables reuse of code in many programs
- Improves readability of the program
- Improves maintainability of the program
- Makes it easy to extend your program
Access Rights : The Public and Private keywords specify public or private intra module-level access rights, respectively. If neither is given, the function defaults to public access (Public).
Calling Convention : Calling convention, or the order in which arguments are pushed and popped from the stack during function calls, is specified with the cdecl, pascal and stdcall keywords. If none is given, the function uses the standard convention by default (stdcall).
Passing Arguments : Functions may receive one or more variables, or arguments, when called. These arguments are listed as parameters in the parameter_list. The ByRef and ByVal keywords specify whether the argument will be passed by reference or by value, respectively. The argument's type is given by "As type" following the parameter. If a parameter in the declaration is given a default value, the parameter is optional. Array parameters are specified by following an identifier with an empty parenthesis. Note that array parameters are always ByRef and the ByRef keyword is neither required nor allowed for array parameters. When calling a function with an array argument the parenthesis must be supplied there too; see the examples.
Overloaded Functions : An overloaded function may share the same name (identifier) as another with a different signature. The Overload keyword specifies that a function may be overloaded. A function must be defined - or declared - using the Overload keyword prior to any functions that overload them.
Returning values : return_type specifies the data type returned by a function upon exit. If no data type is specified, then the function will return the default data type, which will be Integer unless set to another data type using DefSng, DefDbl, DefStr, etc. Functions can return values using three methods: the Return keyword followed by a value exits the function immediately, and returns that value to the caller. Functions can also return values by assigning the Function keyword or the function's identifier to the desired return value (but Function keyword or function's identifier does not allow to evaluate the current assigned value). The latter two methods do not cause the function to exit, however. Return keyword mixed with Function= keyword or function's identifier= or Exit Function keyword in a same function is unsupported when returning objects with constructors. Since functions return values, function calls evaluate to expressions. Thus, function calls can be made wherever an expression is expected, like in Assignments or If statements. Functions can also return references by specifying Byref As return_type.
Warning: Whatever the output branch used, the return value must be always defined, otherwise an unexpected behavior may occur.
Local Variable Preservation : The Static keyword specifies that a function's locally declared variables are preserved between function calls. Upon entering a function defined with Static, local variables have the same value as when the function was last called.
Global Variable Access: To access duplicated symbols defined as global outside the function body, add one or preferably two dot(s) as prefix: .SomeSymbol or preferably ..SomeSymbol (or only ..SomeSymbol if inside a With..End With block).
When calling a function, parentheses surrounding the argument list (if any) are required only for function calls in expressions. If there is no argument to pass, the parentheses become optional, but it is a common convention to place empty parentheses '()' after the function name, to signify a function call.
Warning for 64-bit compiler only: See the Identifier Rules page for the choice of user procedure identifier names (and specially the 'Platform Differences' paragraph).
- In the -lang fb dialect,
ByValis the default parameter passing convention for all built-in types exceptString; String and user-definedTypes are passedByRefby default. In the -lang qb and -lang fblite dialects,ByRefis the default parameter passing convention.
In the -lang qb dialect, the name of the function must be used in an assignment to specify the return value. UsingFunction = ..." to specify the return value may not be used.
In the -lang qb and -lang fblite dialects,Returnmay only be used to return a value whenOption Gosubis off. In -lang qb, this must be done explicitly using theOption Nogosubstatement.
- Parameters can be optional in FreeBASIC.
In QBASIC, the return type could only specified with a suffix, not with
AS TYPE, and only allowed functions to return a built-in type.
Return value can now be specified by aReturnstatement.
FunctionOverloading is supported in FreeBASIC.
The return value of functions can be ignored in the calling code.
'' This program demonstrates the declaration of a function
'' and returning a value using Return command
Declare Function ReturnTen () As Integer
Print ReturnTen () '' ReturnTen returns an integer by default.
Function ReturnTen() As Integer
Return 10
End Function'' This program demonstrates the declaration of a function
'' and returning a value using assignment to function name
Declare Function ReturnTen () As Integer
Print ReturnTen () '' ReturnTen returns an integer by default.
Function ReturnTen() As Integer
ReturnTen = 10
End Function'' This program demonstrates function overloading.
'' The overloaded functions must be FIRST.
Declare Function ReturnTen Overload (a As Single) As Integer
Declare Function ReturnTen Overload (a As String) As Integer
Declare Function ReturnTen (a As Integer) As Integer
Print ReturnTen (10.000!) '' ReturnTen will take a single and return an integer
Print ReturnTen (10) '' ReturnTen will take an integer and return an integer
Print ReturnTen ("10") '' ReturnTen will take a string and return an integer
Function ReturnTen Overload (a As Single) As Integer
Return Int(a)
End Function
Function ReturnTen Overload (a As String) As Integer
Return Val(a)
End Function
Function ReturnTen (a As Integer) As Integer
Return a
End Function'' The following example demonstrates optional parameters.
Function TestFunc(P As String = "Default") As String
Return P
End Function
Print TestFunc("Testing:")
Print TestFunc'' This example shows how to declare and call
'' functions taking array arguments.
Function x(b() As Double) As Integer
x = UBound(b)-LBound(b)+1
End Function
Dim a(1 To 10) As Double
Print x(a())
Dim c(10 To 20) As Double
Print x(c())Category: Procedures
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing