Why default parameter cannot be an address?

Just starting out? Need help? Post your questions and find answers here.
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Why default parameter cannot be an address?

Post by swhite »

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: Select all

    Structure Param
        value1.s
        value2.i
   EndStructure

   Global gParam.Param

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

   
Simon White
dCipher Computing
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3870
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Why default parameter cannot be an address?

Post by wilbert »

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: Select all

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
Windows (x64)
Raspberry Pi OS (Arm64)
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Why default parameter cannot be an address?

Post by Dude »

Discussed back in 2009 (see Freak's answers): viewtopic.php?f=3&t=39108
swhite
Enthusiast
Enthusiast
Posts: 726
Joined: Thu May 21, 2009 6:56 pm

Re: Why default parameter cannot be an address?

Post by swhite »

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: Select all

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
Simon White
dCipher Computing
Post Reply