Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / arrays passed to functions

Author
Message
jayell leedham
18
Years of Service
User Offline
Joined: 24th Jan 2006
Location: North Yorkshire
Posted: 8th Apr 2007 13:51
I understand that arrays are automatically global if declared in the main programme. In a function, I want to carry out an action on 1 of several arrays, depending on the conditions in the main prog. eg (only for 2 arrays to show the problem):-
dim one(100):dim two(100)
a=1
answer=add(a)
print answer:wait key:end

function add(num)
if num=1:one(1)=num:one(2)=num+1:one(3)=num+2
else
two(1)=num:two(2)=num+1:two(3)=num+2
endif:endfunction 0
rem there could be many more "a" values
This doesn't work anyway! Why is that?
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 8th Apr 2007 15:14
There are two problems:
1. You need to say what answer you want from the function. i.e. within the function, declare something like 'answer=one(1)'
2. You need to use a variable to pass the number back to your main program instead of 0. Use 'endfunction answer'.

This should work for you.
Coder#05
18
Years of Service
User Offline
Joined: 26th Oct 2005
Location: Denmark
Posted: 8th Apr 2007 15:45
The only value returned from your function (add) is 0
because off ENDFUNCTION 0.
I think what you want is :
ENDFUNCTION name_off_the_variable_that_holds_the_value_you_need

Hope it helps a bit.

easy comes easy goes
jayell leedham
18
Years of Service
User Offline
Joined: 24th Jan 2006
Location: North Yorkshire
Posted: 8th Apr 2007 21:39
Thanks LBFN and Coder#05.I've simplified the code below. I do not need to return a value as the action is to alter "beam(2)" or "sun(2)"(both elements of global arrays).
It still does not run! It fails on "answer=add(a)".
Anyway, if there were, say 7 arrays to change, depending on the value of "a", there would be a lot of duplicated code and if-elses (that was my original problem).
dim beam(100):dim sun(100)
beam(2)=999:sun(2)=999
a=1
answer=add(a)
print beam(2)," ",sun(2):wait key:end

function add(num)
if num=1:beam(2)=5
else
sun(2)=5
endif:endfunction 0
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 8th Apr 2007 21:49 Edited at: 8th Apr 2007 21:51
There's no need for the function to return a value. You could do this:





And call it like:



Anyway, the problem with your code is that endfunction needs its own line if it is returning a value for some reason. So, this works (returns a value):


function add(num)
if num=1:beam(2)=5
else
sun(2)=5
endif
endfunction 0


and so does this (doesn't return a value):


function add(num)
if num=1:beam(2)=5
else
sun(2)=5
endif:endfunction


But what you're currently doing does not work, which is this:


function add(num)
if num=1:beam(2)=5
else
sun(2)=5
endif:endfunction





Good luck

jayell leedham
18
Years of Service
User Offline
Joined: 24th Jan 2006
Location: North Yorkshire
Posted: 9th Apr 2007 10:56
Thanks for your help guys.
The endfunction to be on its own line threw me.

Login to post a reply

Server time is: 2024-09-25 21:26:23
Your offset time is: 2024-09-25 21:26:23