Page 1 of 1

using name_declared in if statement

Posted: Tue Jun 07, 2016 9:34 am
by chris
so I was testing some code when I came across a strange circumstance...
if I do

Code: Select all

 newvar = 10
newvar has a value of 0 after this code!
I am not really sure what is going on as anything inside the if statement does not seem to be executed, but newvar is "declared" (to 0).
So I can understand that if the if statement is false variables inside are put in the symbol table with default value of 0. But then why isn't the if statement true?

if I instead do this:

Code: Select all

type = name_declared("newvar")
if(type !=5){
 newvar = 10
newvar has a value of 10...

so how do the code blocks differ actually?

further, the behaviour is different in a procedure.
That is, every variable is in the symbol table immediately after the function is defined, but before the function can be called. So everything using name_declared within the procedure/function with a variable defined within the function is on a declared variable.

I know name_declared shouldn't often be used, but it is useful for testing a file with default/test values if it isn't loaded from another file.
And I know there are alternatives such as using Python, but I found this strange and wondered why...

some more test code:

Code: Select all

printf("declared a: %g\n",name_declared("a"))			// -> declared a: 0
printf("name_declared(a)!=5  %g\n",name_declared("a")!=5) // -> name_declared(a)!=5   1
	printf("in if a\n")		//-> prints
	a = 10
	printf("value: %g\n",a)	//-> prints
printf("declared a: %g, value: %g\n",name_declared("a"),a) //-> declared a: 5, value: 10

printf("declared b: %g\n",name_declared("b")) //-> declared b: 1
printf("name_declared(b)!=5  %g\n",name_declared("b")!=5) // -> name_declared(b)!=5   1
	printf("in if b\n")		// doesn't print
	b = 10
	printf("value: %g\n",b)	// doesn't print
printf("declared b: %g, value: %g\n",name_declared("b"),b)	//-> declared b: 5, value: 0

	printf("in if c\n") 	//doesn't print
	c = 10
	printf("declared c: %g, value: %g\n",name_declared("c"),c) //doesn't print

proc testProc(){
	printf("declared d: %g\n",name_declared("d"))			// -> declared d: 5
	printf("name_declared(d)!=5  %g\n",name_declared("d")!=5) // -> name_declared(d)!=5   0
		printf("in if d\n")		// doesn't print
		d = 10
		printf("value: %g\n",d)	// doesn't print
	printf("declared d: %g, value: %g\n",name_declared("d"),d) //-> declared d: 5, value: 0

	printf("declared e: %g\n",name_declared("e")) //-> declared e: 1
		printf("in if e\n")		// doesn't print
		e = 10
		printf("value: %g\n",e)	// doesn't print
	printf("declared e: %g, value: %g\n",name_declared("e"),e)	//-> declared e: 5, value: 0

		printf("in if f\n") 	//doesn't print
		f = 10
		printf("declared f: %g, value: %g\n",name_declared("f"),f) //doesn't print

Re: using name_declared in if statement

Posted: Wed Jun 08, 2016 1:20 am
by hines
In your first fragment name became defined during parsing (default value of 0) of the "if" statement and then "name=10" was never executed.
The idiom to avoid this
if (name_declared("name") == 0) { execute("name = ...") }
then name becomes declared only when the execute statement is actually executed.

Re: using name_declared in if statement

Posted: Wed Jun 08, 2016 8:55 am
by chris