Declare & default values usage

Mac OSX specific forum
WilliamL
Addict
Addict
Posts: 1259
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Declare & default values usage

Post by WilliamL »

Maybe I'm missing something obvious but I can't get this to work. No matter how I use Declare, I can't use a default value.

Code: Select all

Declare a(a,b,c) ; incorrect number of parameters
;Declare a(a,b,c=0) ; Declare doesn't match real procedure

  a(10, 12)      ; 2 will be used as default value for 3rd parameter
  a(10, 12, 15) 
  
Procedure a(a, b, c=2)
    Debug c
  EndProcedure
MacBook Pro-M1 (2021), Tahoe 26.2, PB 6.30b6
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Declare & default values usage

Post by wilbert »

Code: Select all

Declare a(a,b,c=2)
WilliamL
Addict
Addict
Posts: 1259
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Declare & default values usage

Post by WilliamL »

So it's just a case of c not be equal to zero in the Declare statement? I'm not sure what the value of c has to do with the Declare statement... but, whatever.

Thanks Wilbert!
MacBook Pro-M1 (2021), Tahoe 26.2, PB 6.30b6
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Declare & default values usage

Post by luis »

If you think about it makes sense, the declare it's a way to anticipate to the compiler the prototype of the procedure.

So if the proc params are (a,b,c=2) the declare must be the same, else you are giving the compiler a wrong info.
"Have you tried turning it off and on again ?"
WilliamL
Addict
Addict
Posts: 1259
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Declare & default values usage

Post by WilliamL »

Hi Luis! How have you been?

Oh, maybe I'm just having one of those days! :oops:

[edit]
Oh, now I see! The Declare value has to be the same value as the default value in the Procedure. Doh, I was thinking the variable in the Declare was just a placeholder variable and the value didn't matter (kinda like the first two variables). Ok, lesson learned. :)
MacBook Pro-M1 (2021), Tahoe 26.2, PB 6.30b6
User avatar
Demivec
Addict
Addict
Posts: 4283
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Declare & default values usage

Post by Demivec »

WilliamL wrote:So it's just a case of c not be equal to zero in the Declare statement? I'm not sure what the value of c has to do with the Declare statement... but, whatever.
A Declare statement and a Procedure statement need to match in several important ways. They both need to include the same return type. They both need to have parameters of the same type and order listed. If the parameters are optional they need to specify matching default values.

A Declare statement and a Procedure statement can differ in several unimportant ways. As long as the previous conditions regarding type and order were met the parameters can have different names. Also, if a parameter is a pointer, it's structure type is ignored for the purposes of matching since both parameters contain integer-sized values.
Post Reply