POV-Ray : Newsgroups : povray.general : Parameters to functions used in expressions ... Server Time
5 Nov 2024 16:41:49 EST (-0500)
  Parameters to functions used in expressions ... (Message 1 to 5 of 5)  
From: Neil Kolban
Subject: Parameters to functions used in expressions ...
Date: 18 Jan 2005 23:41:49
Message: <41ede50d@news.povray.org>
Folks,

I am still learning functions ... I wanted to create something like:

#declare foo = function(a,b,c) {
   #if (a>0.0)
      #declare a=a+1;
   #end
a+b+c
}

My problem seems to be that the formal parameters supplied to the function 
don't seem to be in scope for the expressions.  It is as thought they were 
not declared.  When I try and run this, I get:

-----

File: C:\Documents and Settings\kolban\My Documents\My 
3D\Projects\Tiles\test.inc  Line: 2
#declare foo = function(a,b,c) {

#if (a <----ERROR

Parse Error: Expected 'numeric expression', undeclared identifier 'a' found 
instead

Returned from renderer with error status

-----

Am I not allowed to use function parameters in the conditionals within my 
function body?

Neil


Post a reply to this message

From: Slime
Subject: Re: Parameters to functions used in expressions ...
Date: 18 Jan 2005 23:59:04
Message: <41ede918$1@news.povray.org>
> My problem seems to be that the formal parameters supplied to the function
> don't seem to be in scope for the expressions.  It is as thought they were
> not declared.

They aren't. Commands beginning with # are evaluated at parse time in order
to get the tokens which are used to generate the objects they surround: in
this case, the function. So as the # commands are evaluated, the function
doesn't exist yet, and once the function is finished being created, the #
commands are already taken care of and are never seen again.

If you want to have an "if"-type structure within a function, you can use
the select() function (untested code):

#declare foo = function(a,b,c) {
    select(a,
        (a+1)+b+c, // if a >= 0
        a+b+c // otherwise
    )
}

See the documentation for more thorough information on select().

Also note that you can't define temporary variables within a function (that
is, there's no equivalent for #declare). You can, however, call other
functions and pass them whatever you want, which is often a decent
replacement:

#declare bar = function(a,b,c) {
    a+b+c // or something more complicated
}
#declare foo = function(a,b,c) {
    select(a, bar(a+1, b, c), bar(a, b, c))
}

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Vincent LE CHEVALIER
Subject: Re: Parameters to functions used in expressions ...
Date: 19 Jan 2005 03:33:31
Message: <41ee1b5b$1@news.povray.org>
Slime wrote:
>>My problem seems to be that the formal parameters supplied to the function
>>don't seem to be in scope for the expressions.  It is as thought they were
>>not declared.
> 
> 
> They aren't. Commands beginning with # are evaluated at parse time in order
> to get the tokens which are used to generate the objects they surround: in
> this case, the function. So as the # commands are evaluated, the function
> doesn't exist yet, and once the function is finished being created, the #
> commands are already taken care of and are never seen again.
> 

Do arrays work in the same way ? I recall having tried to get a value in 
an array, indexed by a parameter of a function (something like 
function(x) {myarray[floor(x)]} ) and never being able to find a way to 
do so...

-- 
Vincent


Post a reply to this message

From: Slime
Subject: Re: Parameters to functions used in expressions ...
Date: 19 Jan 2005 09:09:22
Message: <41ee6a12$1@news.povray.org>
> Do arrays work in the same way ? I recall having tried to get a value in
> an array, indexed by a parameter of a function (something like
> function(x) {myarray[floor(x)]} ) and never being able to find a way to
> do so...

Yes; since array elements are evaluated during parse time, the index of the
array can't be dependant on function arguments which would only be available
when the function is evaluated. The number within the [] brackets must be
something that can be evaluated ahead of time, and then the function will be
equivalent to function(x) {3} or something like that.

(There are ways, however, to construct a function from an SDL array that
will work essentially like a lookup on the array. Unfortunately, the lookup
will be at best an O(log(n)) search instead of an O(1) lookup, and the
function will take more memory than the original array itself.)

 - Slime
 [ http://www.slimeland.com/ ]


Post a reply to this message

From: Vincent LE CHEVALIER
Subject: Re: Parameters to functions used in expressions ...
Date: 19 Jan 2005 11:13:28
Message: <41ee8728@news.povray.org>
Slime wrote:
>>Do arrays work in the same way ? I recall having tried to get a value in
>>an array, indexed by a parameter of a function (something like
>>function(x) {myarray[floor(x)]} ) and never being able to find a way to
>>do so...
> 
> 
> Yes; since array elements are evaluated during parse time, the index of the
> array can't be dependant on function arguments which would only be available
> when the function is evaluated. The number within the [] brackets must be
> something that can be evaluated ahead of time, and then the function will be
> equivalent to function(x) {3} or something like that.
> 
> (There are ways, however, to construct a function from an SDL array that
> will work essentially like a lookup on the array. Unfortunately, the lookup
> will be at best an O(log(n)) search instead of an O(1) lookup, and the
> function will take more memory than the original array itself.)
> 
>  - Slime
>  [ http://www.slimeland.com/ ]
> 
> 
OK, thanks for the insight !

-- 
Vincent


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.