It is currently Sun Nov 18, 2018 5:57 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Why default parameter cannot be an address?
PostPosted: Wed Aug 15, 2018 6:18 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 21, 2009 6:56 pm
Posts: 494
Hi
I tried to create a procedure as follows but the compiler complains of a syntax error. I am assuming that it does not like the @gParam as the default value for the second parameter but it should be a constant expression to my mind.

Code:
    Structure Param
        value1.s
        value2.i
   EndStructure

   Global gParam.Param

    Procedure Mytest(test1.s,*test.param = @gParam)
        *test\Value1 = test1
        debug *test\Value1
    EndProcedure

   


Top
 Profile  
Reply with quote  
 Post subject: Re: Why default parameter cannot be an address?
PostPosted: Wed Aug 15, 2018 6:50 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3260
Location: Netherlands
I think a constant needs to be known at compile time which is not the case for a variable address, even if it's a global one.
The program needs to be loaded first by the OS before a variable gets assigned to a specific address.

As a workaround you could use #Null and replace that inside the procedure
Code:
Structure Param
  value1.s
  value2.i
EndStructure

Global gParam.Param

Procedure Mytest(test1.s,*test.param = #Null)
  If *test = #Null : *test = @gParam : EndIf
  *test\Value1 = test1
  Debug *test\Value1
EndProcedure

_________________
macOS 10.14 Mojave, PB 5.62 x64


Top
 Profile  
Reply with quote  
 Post subject: Re: Why default parameter cannot be an address?
PostPosted: Wed Aug 15, 2018 10:05 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1609
Discussed back in 2009 (see Freak's answers): viewtopic.php?f=3&t=39108


Top
 Profile  
Reply with quote  
 Post subject: Re: Why default parameter cannot be an address?
PostPosted: Thu Aug 16, 2018 2:03 pm 
Offline
Enthusiast
Enthusiast

Joined: Thu May 21, 2009 6:56 pm
Posts: 494
Thanks that makes sense. I was assuming the address was known at compile time which you show is not the case. Your solution will also work fine for me.

wilbert wrote:
I think a constant needs to be known at compile time which is not the case for a variable address, even if it's a global one.
The program needs to be loaded first by the OS before a variable gets assigned to a specific address.

As a workaround you could use #Null and replace that inside the procedure
Code:
Structure Param
  value1.s
  value2.i
EndStructure

Global gParam.Param

Procedure Mytest(test1.s,*test.param = #Null)
  If *test = #Null : *test = @gParam : EndIf
  *test\Value1 = test1
  Debug *test\Value1
EndProcedure


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye