Page 1 of 1

Declare & default values usage

Posted: Sat Feb 11, 2012 6:52 pm
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

Re: Declare & default values usage

Posted: Sat Feb 11, 2012 6:55 pm
by wilbert

Code: Select all

Declare a(a,b,c=2)

Re: Declare & default values usage

Posted: Sat Feb 11, 2012 7:05 pm
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!

Re: Declare & default values usage

Posted: Sat Feb 11, 2012 7:10 pm
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.

Re: Declare & default values usage

Posted: Sat Feb 11, 2012 7:54 pm
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. :)

Re: Declare & default values usage

Posted: Sat Feb 11, 2012 8:06 pm
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.