Variable number of parameters in Subs and Functiions

General FreeBASIC programming questions.
Dean.Hodgson
Posts: 45
Joined: Feb 17, 2010 5:28

Variable number of parameters in Subs and Functiions

Postby Dean.Hodgson » Mar 15, 2010 22:50

Hi,

I have no idea if this is possible or not but I thought I'd ask.

Is it possible for a SUB and/or FUNCTION to have an unknown number of parameters and, if so, is there a way to read them within the function and determine their type? An example: say I wish to have a general-purpose function that reads data from an open binary file. I want to simply list the variables into which data will be read, but the list can vary depending on what is required. (The old ZBasic language and FutureBasic had something like this.) The variables could be any type including string. I am imagining something sort of like this:

SUB ReadFromFile ( ??? )
if possible, determine how many variables are to be used
loop
get variable (address?) and its type, create alias whatever
until all variables have been identified
...rest of code...
END SUB

Or something else that achieves the same thing.

This might be impossible and I know UDT's can be used for this but it is simply an example.

If it doesn't exist, perhaps a developer might consider it?

Thank you to anyone who knows!
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Postby vdecampo » Mar 15, 2010 23:02

... (Ellipsis) Used in place of procedure parameter to pass a variable number of arguments


Syntax

Declare { Sub | Function } proc_name cdecl ( param As datatype, ... )


Description

The ellipsis (three dots "...") is used in procedure declarations and definitions to indicate a variable argument list. A first argument must always be specified and the function must be called with the C calling convention cdecl.


Example


Declare Function FOO cdecl (X As Integer, ...) As Integer




Differences from QB


New to FreeBASIC


See also

cdecl
va_arg
va_first
va_next

Ophelius
Posts: 428
Joined: Feb 26, 2006 1:57

Postby Ophelius » Mar 15, 2010 23:03

duke4e
Posts: 717
Joined: Dec 04, 2005 0:16
Location: Varazdin, Croatia, Europe
Contact:

Postby duke4e » Mar 15, 2010 23:12

If you have a fixed number of cases how the file structure can be formed, consider using function overloading.
Dean.Hodgson
Posts: 45
Joined: Feb 17, 2010 5:28

Postby Dean.Hodgson » Mar 15, 2010 23:30

What is function overloading please? I've seen the term but don't have a good grip on the concept.
Ophelius
Posts: 428
Joined: Feb 26, 2006 1:57

Postby Ophelius » Mar 15, 2010 23:33

Dean.Hodgson
Posts: 45
Joined: Feb 17, 2010 5:28

Postby Dean.Hodgson » Mar 15, 2010 23:54

Thanks for that. Now I understand Overloaded. Much simpler than I thought and very useful. The same sub/function name can be used but with different parameter combinations.
Dean.Hodgson
Posts: 45
Joined: Feb 17, 2010 5:28

Postby Dean.Hodgson » Mar 16, 2010 1:11

Almost out of the woods but not quite!

I am interested in specifying variables that will receive data not only pass it -- e.g. BYREF. Some may also pass by value, however.

An example would be something like the INPUT # command.
From the documentation: Input [;] ["prompt" separator] variable_list

The variables in the list receive the data. I'd like to try to do similar with my own function.

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest