Can functions return arrays?

New to FreeBASIC? Post your questions here.
srvaldez
Posts: 3645
Joined: Sep 25, 2005 21:54

Re: Can functions return arrays?

Post by srvaldez »

thesanman112 wrote:

Code: Select all

function ArrayofNumbers(x as integer, a() as integer,b as integer) as integer export
	dim as integer z
    for z=1 to x
    function = a(z)+b:print a(z)+b
    next z
end function
your function makes no sense, why do you have function = a(z)+b inside a loop?
you can easily work on the array by reference, also you can use lbound(a) and ubound(a) to get the lower and upper dimensions of the array
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

The original question was based on passing an array to a function.....and then someone asked about using the function as a library......just trying to keep it simple for learning about library's......maybe someone can just modify me code to pass the array back outnof the function in the library??
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

sancho2 wrote:There is also the option of wrapping the array in a type and returning that:

Code: Select all

Type MyArray
	As Integer n(1 To 10)
End Type
Function test() As MyArray
	Dim As MyArray t
	For x As Integer = 1 To 10
		t.n(x) = x
	Next
	Return t
End Function

Dim p As MyArray
p = test()

For x As Integer = 1 To 10
	Print p.n(x)
Next

Sleep
This is an excellent example sancho, not hard to follw and would make an excellent example to make a library function.
srvaldez
Posts: 3645
Joined: Sep 25, 2005 21:54

Re: Can functions return arrays?

Post by srvaldez »

hello thesanman112 :-)
apologies if I came across as rude, but what is the function intended to do?

Code: Select all

    for z=1 to x
         a(z) = a(z)+b:print a(z)+b
    next z
is that what mean?
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

it just shows that the complete array has been passed to the function....which is actually a library.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

The line of code was...
Function=a(z)+b: print a(z)+b

Its contained in mydll.dll once you get it compiled..
I added the print statement to show that all the array was being passed to the functiin contained in the library.
Its meant for beginners to learn from.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can functions return arrays?

Post by Tourist Trap »

TeeEmCee wrote:

Code: Select all

function ArrayReturner( ArrayExample() as typeOf(ArrayExample) ) as ARR")
Your last two code blocks appear to be identical. What did you mean?
In the last example the type is the type of the argument array of the function without any possibility for the compiler to know it before the program is running and the argument takes some argument of what or what type. There is not such run-time types in FB. But it's very appealing so I tried it.

What happens if one tries that (below), it's something to be tested:

Code: Select all

dim as integer	arrayExample(100)

declare function ArrayReturner( ArrayExample() as typeOf(ArrayExample) ) as ARR")
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can functions return arrays?

Post by Tourist Trap »

thesanman112 wrote:.....just trying to keep it simple for learning about library's......
See those 2 topics about libaries:
http://www.freebasic.net/forum/viewtopi ... ad#p229708
http://www.freebasic.net/forum/viewtopi ... ad#p227979

The second one shows a usage that may interest you.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

After learning opengl and directx using fb im not sure if i want to look....hehehehe
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can functions return arrays?

Post by Tourist Trap »

thesanman112 wrote:After learning opengl and directx using fb im not sure if i want to look....hehehehe
No problem, using dynamic libraries is not absolutely necessary in general. It's you who mentioned it , but I maybe didn't understand your statement.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

I have been actually curious about library's so when you asked me to make an example I tried...based on the example that comes with FBC package.
I'm more of a numbers based programmer.....although using Library's isn't new to me, I have never actually compiled my own, so what the heck, this is the beginners board so why not throw up some EASY example. Although it may be useless, it at least showed the array was passed to the function.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can functions return arrays?

Post by Tourist Trap »

thesanman112 wrote:why not throw up some EASY example. Although it may be useless, it at least showed the array was passed to the function.
The issue is that I don't believe that a library would pass an array easily! So I'm like a sitting duck until one shows me how ;)
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

tourist trap... did you try the demos I posted, like build the library mydll.dll??

my example I posted shows that the array has been passed to the library...

all you have to do to show that work had been done in the library to the array and returned to the main program is
change the 'print a(z)+b' to a(z)=a(z)+b,
I call the function/library in the main program, dylib.bas with
'v=arrayofnumbers(z,a(),b)', where x is size of array, a() is array, and b is value added to array.

I know it can be difficult to follow other peoples style of coding, I didn't use TYPE, END TYPE of any kind
or pointers to keep it simple.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Can functions return arrays?

Post by Tourist Trap »

thesanman112 wrote:my example I posted shows that the array has been passed to the library...
Oh sorry! Maybe I see what is going on. Passing an argument is a thing, but having it returned as one would return a packet to someone by mail, it's something else. The syntax function F(argument_passed) as returned_type , is for me of the second sort. For a library I would expect some data packet returned after you discarded the library for instance, or anything like that, like when you end a program by the instruction "END 100" so that 100 is returned.

So we were just not talking about exactly the same thing. But you are right, a function embedded in a library is still able to take an array as argument, and it's byref, and so on, just as usual. But the return value is still not an array. At least we will have to dig out a little more.

Thanks anyway for clarifying this point.
thesanman112 wrote: I know it can be difficult to follow other peoples style of coding, I didn't use TYPE, END TYPE of any kind
or pointers to keep it simple.
I use often user defined types, but sincerely not because they are easy so much to design. I use them because the maintenance in the long term is very reduced. So I would agree with you if you meant that UDTs are not really a simplification, above all when we come to small programs that could be written in a far simpler way.
thesanman112
Posts: 538
Joined: Jul 15, 2005 4:13

Re: Can functions return arrays?

Post by thesanman112 »

I don't follow, the return value is indeed not an array, but the whole array is returned to main program just as though you did the work to the array in the main program, I think I'm understanding you correctly no??

like ' v=arrayofnumbers(x,a(),b)

where v is not an array...its simply a returned value 0 or 1 I believe... almost like checking if function/library performed operation.


note: we need to stop editing our posts!!! hahahaha
Post Reply