Wiki source for KeyPgDots


Show raw source

{{fbdoc item="title" value="... (Ellipsis)"}}----
Used in place of procedure parameter to pass a variable number of arguments, or as the upper bound in an array declaration to denote that the number of elements will be determined by the initializer.

{{fbdoc item="syntax"}}##
[[KeyPgDeclare|declare]] { [[KeyPgSub|sub]] | [[KeyPgFunction|function]] } //proc_name// [[KeyPgCdecl|cdecl]] ( //param_list//, **...** ) { | [ [[KeyPgByrefFunction|Byref]] ] [[KeyPgAs|As]] //return_type// }

[[KeyPgPpdefine|#define]] //identifier//( [ //parameters//, ] //variadic_parameter//**...** ) //body//

[[KeyPgDim|dim]] //array_symbol// ([//lbound// [[KeyPgTo|to]]] **...**) [[[KeyPgAs|as]] [[DataType|datatype]]] => { //expression_list// }
##
{{fbdoc item="desc"}}
__Variadic Procedures__

The ellipsis (three dots, ##**...**##) is used in procedure declarations and definitions to indicate a variable argument list.

A first argument (at least) must always be specified and the procedure must be called with the C calling convention ##[[KeyPgCdecl|cdecl]]##.

In the procedure body, ##[[KeyPgCvaList|cva_list]]## data type, and ##[[KeyPgCvaArg|cva_arg]]## macro can be used to expand the ellipsis parameter (##**...**##) to obtain the values of the arguments passed to the variadic procedure. The argument list, once initialized with ##[[KeyPgCvaStart|cva_start]]## or copied with ##[[KeyPgCvaCopy|cva_copy]]##, can be passed to another procedure taking a ##[[KeyPgCvaList|cva_list]]## parameter.

On some targets, for backwards compatibility, ##[[KeyPgVaFirst|va_first]]##, ##[[KeyPgVaArg|va_arg]]## and ##[[KeyPgVaNext|va_next]]## can still be used to handle the variable arguments.

Only numeric types and pointers are supported as variable arguments (all bytes and shorts passed on variable arguments are implicitly converted to integers, all singles passed on variable arguments are implicitly converted to doubles). ##[[KeyPgString|String]]##s can be passed, in which case a ##[[KeyPgZstring|Zstring]] [[KeyPgPtr|Ptr]]## to the string data is taken.

A variadic procedure name can never be overloaded.

__Variadic Macros__

Using an ellipsis behind the last parameter in a ##[[KeyPgPpdefine|#define]]## or ##[[KeyPgPpmacro|#macro]]## declaration allows creation of a variadic macro. This means it is possible to pass any number of arguments to the //variadic_parameter//, which can be used in the //body// as if it was a normal macro parameter. The //variadic_parameter// will expand to the full list of arguments passed to it, including commas, and can also be completely empty.

**Note:** To distinguish between the different arguments passed by //variadic_parameter//, you can first convert //variadic_parameter// to a string using the ##[[KeyPgOpPpStringize|Operator # (Preprocessor Stringize)]]##, then differentiate in this string (//#variadic_parameter//) each passed argument by locating the separators (usually a comma).

__Array Upper Bound__

Using an ellipsis in place of the upper bound in an array declaration causes the upper bound to be set according to the data that appears in the ##//expression_list//##. When the ellipsis is used in this manner, an initializer must appear, and cannot be ##[[KeyPgAny|Any]]##.


{{fbdoc item="ex"}}
{{fbdoc item="filename" value="examples/manual/procs/ellipsis.bas"}}%%(freebasic)
Declare Function foo Cdecl (x As Integer, ...) As Integer
%%

{{fbdoc item="filename" value="examples/manual/array/ellipsis.bas"}}%%(freebasic)
Dim As Integer myarray(0 To ...) = {0, 1, 2, 3}
Print Lbound(myarray), Ubound(myarray) '' 0, 3
%%

{{fbdoc item="filename" value="examples/manual/prepro/variadic.bas"}}%%(freebasic)
'' Using a variadic macro to wrap a variadic function
#include "crt.bi"
#define eprintf(format, args...) fprintf(stderr, format, args)
eprintf(!"Hello from printf: %i %s %i\n", 5, "test", 123)

'' LISP-like accessors allowing to modify comma-separated lists
#define car(a, b...) a
#define cdr(a, b...) b
%%

{{fbdoc item="diff"}}
- New to ""FreeBASIC""

{{fbdoc item="see"}}
- ##[[KeyPgCdecl|cdecl]]##
- ##[[KeyPgCvaList|cva_list]]##
- ##[[KeyPgCvaArg|cva_arg]]##
- ##[[KeyPgVaArg|va_arg]]##
- ##[[KeyPgVaFirst|va_first]]##
- ##[[KeyPgVaNext|va_next]]##
- ##[[KeyPgDim|dim]]##
- ##[[KeyPgStatic|static]]##
- ##[[KeyPgPpdefine|#define]]##

{{fbdoc item="back" value="CatPgProcedures|Procedures"}}{{fbdoc item="back" value="CatPgVarArg|Variable Argument list"}}
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki



sf.net phatcode