POV-Ray : Newsgroups : povray.advanced-users : How to return multiple values from a macro? Server Time
24 Nov 2024 13:04:23 EST (-0500)
  How to return multiple values from a macro? (Message 1 to 4 of 4)  
From: Jos leys
Subject: How to return multiple values from a macro?
Date: 3 Sep 2006 17:35:00
Message: <web.44fb4a6b3c2bacd6f4015f620@news.povray.org>
As an example, take the simple case where I have two values A and B, and I
need their sum and their product. Is there a way to do this with one macro?
(my actual problem is a lot more complex,(rotation in four dimensions) but
the principle is the same..)

#macro sum_product(A,B)
  #local sum=A+B
  #local product=A*B
.......?
#end

// now how do I read sum and product?
Is this at all possible?


Post a reply to this message

From: Tim Attwood
Subject: Re: How to return multiple values from a macro?
Date: 3 Sep 2006 18:55:36
Message: <44fb5d68@news.povray.org>
> As an example, take the simple case where I have two values A and B, and I
> need their sum and their product. Is there a way to do this with one 
> macro?

You can return values by parameter, but shouldn't because it's bugged...
IE #macro Foo(A, B, result1, result2) is bad, but might be fixed someday.

So that leaves passing values by substitution, which perty much limits you
to returning one variable. However, there are types of variables that have
more than one value, most notably vectors. You can fit up to 4 floats in a
vector and later access them with the .x,.y,.z, and .t operators. Or you can
return an array that holds the results.

#macro sum_product(A,B)
  #local mysum = A+B;
  #local myproduct = A*B;
  #local result = array[2]{mysum,myproduct};
  result
#end

#declare sum_product_result = sum_product(2, 3);
#declare sum_result = sum_product_result[0];
#declare product_result = sum_product_result[1];


Post a reply to this message

From: Chris B
Subject: Re: How to return multiple values from a macro?
Date: 3 Sep 2006 19:15:20
Message: <44fb6208$1@news.povray.org>
"Jos leys" <jos### [at] pandorabe> wrote in message 
news:web.44fb4a6b3c2bacd6f4015f620@news.povray.org...
> As an example, take the simple case where I have two values A and B, and I
> need their sum and their product. Is there a way to do this with one 
> macro?
> (my actual problem is a lot more complex,(rotation in four dimensions) but
> the principle is the same..)
>
> #macro sum_product(A,B)
>  #local sum=A+B
>  #local product=A*B
> .......?
> #end
>
> // now how do I read sum and product?
> Is this at all possible?
>

If you use #declare instead of #local then the values will be available from 
outside of the macro.
#local specifically limits the scope of the variables whereas #declare gives 
a global context.
Then you won't need to return anything if you don't want to.

Regards,
Chris B.


Post a reply to this message

From: Jos leys
Subject: Re: How to return multiple values from a macro?
Date: 4 Sep 2006 01:40:00
Message: <web.44fbbbaf53357596f4015f620@news.povray.org>
"Tim Attwood" <tim### [at] comcastnet> wrote:
>... Or you can return an array that holds the results.
>

Thanks! This should work. ( I'm passing two complex numbers to the macro and
I need two altered complex numbers back)

Jos


Post a reply to this message

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