Gamestudio Links
Zorro Links
Newest Posts
Newbie Questions
by fairtrader. 12/05/23 14:22
Zorro Trader GPT
by TipmyPip. 12/04/23 11:34
Square root rule
by Smallz. 12/02/23 09:15
RTest not found error
by TipmyPip. 12/01/23 21:43
neural function for Python to [Train]
by TipmyPip. 12/01/23 14:47
Xor Memory Problem.
by TipmyPip. 11/28/23 14:23
Training with command line parameters
by TipmyPip. 11/26/23 08:42
Combine USD & BTC Pairs In Asset Loop
by TipmyPip. 11/26/23 08:30
AUM Magazine
Latest Screens
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Who's Online Now
0 registered members (), 635 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
fairtrader, hus, Vurtis, Harry5, KelvinC
19019 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Calling function with parameters??? #214474
07/04/08 19:31
07/04/08 19:31
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline OP
Serious User
DJBMASTER  Offline OP
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
Hi i've been trying to make a function that can accept parameters so i don't have to define seperate functions. I am having a couple of problems.

Scenerio 1

I have this...
Code:
 
function beep_engine()
{	
beep();	
}

function main()
{
on_space=beep_engine;
}


Now if i press space, the engine will beep as expected. It beeps everytime i press space. No problems here.

Scenerio 2

Now i make a function that can determine the number of beeps...

Code:
 
function beep_engine(num_beeps)
{	
if(num_beeps==1){beep();}
if(num_beeps==2){beep();beep();}	
if(num_beeps==3){beep();beep();beep();}	
}

function main()
{
on_space=beep_engine(3);
}


When i run it, the engine beeps 3 times as soon as it has loaded and if i press space the engine crashes.

Am i missing something fundamental about functions? I have read through the manual on function parameters but it doesn't give any examples when pressing a key.

I also don't understand how on_space=beep_engine; is not run when the engine is opened, yet on_space=beep_engine(3); is automatically ran.

A final question to my stupidity>

How come i can press the space key several times when using
on_space=beep_engine; and the engine will still beep. I thought it would have to be inside a loop of some sort for the key to be continuously checked.

I know these must be stupid questions, lol.

Thanks in advanced.



Last edited by DJBMASTER; 07/04/08 19:32.
Re: Calling function with parameters??? [Re: DJBMASTER] #214478
07/04/08 19:43
07/04/08 19:43
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline OP
Serious User
DJBMASTER  Offline OP
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
hmmm has it got something to do with main(). Because that is the entry point?

If i create a seperate function >

function tester()
{
beep_engine(3);
}

and use...

function main()
{
on_space=tester;
}

it works, but again this is creating a uneeded function isn't it? What is soo special about main() that it runs the function straight away even if it is on_space?

Last edited by DJBMASTER; 07/04/08 19:43.
Re: Calling function with parameters??? [Re: DJBMASTER] #214480
07/04/08 19:45
07/04/08 19:45
Joined: Oct 2006
Posts: 873
S
Shadow969 Offline
User
Shadow969  Offline
User
S

Joined: Oct 2006
Posts: 873
you cannot use fuction with parameters with on_key. create a function like

function triple_beep()
{
beep_engine(3);
}

and assign it to space key

function main()
{
on_space=triple_beep();
}

EDIT: you was faster smile it's not about main function, it's about setting on_key to a function pointer. correct me if i'm wrong smile

Last edited by Shadow969; 07/04/08 19:47.
Re: Calling function with parameters??? [Re: Shadow969] #214482
07/04/08 19:49
07/04/08 19:49
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline OP
Serious User
DJBMASTER  Offline OP
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
even that function you posted will still run as soon as the engine is opened. It will beep 3 times and crashes the engine if you press space.

If on_space=triple_beep; is used instead of on_space=triple_beep(); it wont run automatically and will only run when space is pressed.

This is what i don't understand > why it runs straight away.

Re: Calling function with parameters??? [Re: DJBMASTER] #214483
07/04/08 19:59
07/04/08 19:59
Joined: Dec 2005
Posts: 116
T
tD_Datura_v Offline
Member
tD_Datura_v  Offline
Member
T

Joined: Dec 2005
Posts: 116
del::invalid cat::


Re: Calling function with parameters??? [Re: DJBMASTER] #214484
07/04/08 20:01
07/04/08 20:01
Joined: Oct 2006
Posts: 873
S
Shadow969 Offline
User
Shadow969  Offline
User
S

Joined: Oct 2006
Posts: 873
my bad, it's correct without (). dunno why it starts right away, i believe compiler should output an error message

Re: Calling function with parameters??? [Re: Shadow969] #214486
07/04/08 20:17
07/04/08 20:17
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline OP
Serious User
DJBMASTER  Offline OP
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
Yeh that is the reason i posted this. It doesn't output a message, i'm trying to understand a bit more about how functions work. Maybe i should ask JCL why this happens.

Re: Calling function with parameters??? [Re: DJBMASTER] #214490
07/04/08 20:25
07/04/08 20:25
Joined: Dec 2005
Posts: 116
T
tD_Datura_v Offline
Member
tD_Datura_v  Offline
Member
T

Joined: Dec 2005
Posts: 116
del::invalid cat::


Re: Calling function with parameters??? [Re: tD_Datura_v] #214526
07/05/08 05:21
07/05/08 05:21
Joined: Dec 2005
Posts: 478
India
M
msl_manni Offline
Senior Member
msl_manni  Offline
Senior Member
M

Joined: Dec 2005
Posts: 478
India
You cant pass parameter and such. The statement
on_space=beep_engine(3);
is totally invalid.
on_space=beep_engine;
the above statement is only the way you can use it.

when you use "on_space=beep_engine(3);" on_space gets the value of return from executing the function beep_engine.

but if you use "on_space=beep_engine;" then the pointer or address of beep_engine is stored in the on_space.

So now you can understand why you get errors. Both are totally different ways of initiallising the on_space, and only second method is the correct way to do it.LOL.




My Specialities Limited.
Re: Calling function with parameters??? [Re: msl_manni] #214543
07/05/08 09:58
07/05/08 09:58
Joined: Dec 2005
Posts: 116
T
tD_Datura_v Offline
Member
tD_Datura_v  Offline
Member
T

Joined: Dec 2005
Posts: 116
del::invalid cat::

Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1