POV-Ray : Newsgroups : povray.advanced-users : Macro and parameters Server Time
2 Dec 2025 21:45:11 EST (-0500)
  Macro and parameters (Message 1 to 8 of 8)  
From: kurtz le pirate
Subject: Macro and parameters
Date: 28 Nov 2025 11:07:13
Message: <6929c8b1$1@news.povray.org>
Hello,


I just spent a long, long time debugging my code.

For example, this macro :

#macro binomial_coefficient(n, k)

   #local result = 0.0;

   #if ( k<0 | k>n)
     #local result = 0.0;

   #elseif (k=0 | k=n)
     #local result = 1.0;

   #else
     #local result = 1.0;
     #if ( k > n-k )
       #local k = n - k;
     #end
     #local i = 0;
     #while ( i < k )
       #local result = result*(n-i);
       #local result = result/(i+1);
       #local i = i + 1;
     #end
   #end

   // return scalar value
   result
#end



At first glance, nothing special, but there is a problem, a big problem, 
that I hadn't noticed :(


Can you see it too? Well done.


I also program in C, so perhaps that's where the confusion comes from.
But I'll explain it anyway.


The annoying part is this one :

     #if ( k > n-k )
       #local k = n - k;
     #end

... and so, the "#local k =" led me to believe I was using a local 
variable. Well, not at all. "k" represents the variable passed as a 
parameter and its value is modified in the part that called this macro.

By simplifying :

#declare n = 5;
#declare k = 3;
#declare r = binomial_coefficient(n,k);
...and here k = 2, not 3 !


Therefore, be careful not to use a macro parameter in an assignment.

Do you agree with me, or have I misunderstood something again?



-- 
kurtz le pirate
compagnie de la banquise


Post a reply to this message

From: Leroy
Subject: Re: Macro and parameters
Date: 28 Nov 2025 12:45:00
Message: <web.6929df248e84ac71500040e3f712fc00@news.povray.org>
kurtz le pirate <kur### [at] freefr> wrote:
> Hello,
>
>
> I just spent a long, long time debugging my code.
>
> For example, this macro :
>
> #macro binomial_coefficient(n, k)
>
>    #local result = 0.0;
>
>    #if ( k<0 | k>n)
>      #local result = 0.0;
>
>    #elseif (k=0 | k=n)
>      #local result = 1.0;
>
>    #else
>      #local result = 1.0;
>      #if ( k > n-k )
>        #local k = n - k;
>      #end
>      #local i = 0;
>      #while ( i < k )
>        #local result = result*(n-i);
>        #local result = result/(i+1);
>        #local i = i + 1;
>      #end
>    #end
>
>    // return scalar value
>    result
> #end
>
>
>
> At first glance, nothing special, but there is a problem, a big problem,
> that I hadn't noticed :(
>
>
> Can you see it too? Well done.
>
>
> I also program in C, so perhaps that's where the confusion comes from.
> But I'll explain it anyway.
>
>
> The annoying part is this one :
>
>      #if ( k > n-k )
>        #local k = n - k;
>      #end
>
> ... and so, the "#local k =" led me to believe I was using a local
> variable. Well, not at all. "k" represents the variable passed as a
> parameter and its value is modified in the part that called this macro.
>
> By simplifying :
>
> #declare n = 5;
> #declare k = 3;
> #declare r = binomial_coefficient(n,k);
> ...and here k = 2, not 3 !
>
>
> Therefore, be careful not to use a macro parameter in an assignment.
>
> Do you agree with me, or have I misunderstood something again?
> --
> kurtz le pirate
> compagnie de la banquise


I agree! I program in C++ and POV macros and variables are very different!

Try this macro.

#macro ts(R)
 #local R=sphere{0,1 pigment{Gold}}
#end
#declare P=5; ts(P)
object{P}

Have Fun!


Post a reply to this message

From: Bald Eagle
Subject: Re: Macro and parameters
Date: 28 Nov 2025 14:45:00
Message: <web.6929fa968e84ac71d33494a025979125@news.povray.org>
You can pass by value or pass by reference.

https://news.povray.org/povray.advanced-users/thread/%3Cweb.63dd8ba6afaac3401f9dae3025979125%40news.povray.org%3E/

It's a recurring trap.


Post a reply to this message

From: Cousin Ricky
Subject: Re: Macro and parameters
Date: 28 Nov 2025 20:05:46
Message: <692a46ea$1@news.povray.org>
On 2025-11-28 12:07 (-4), kurtz le pirate wrote:
> 
> I just spent a long, long time debugging my code.
> 
> For example, this macro :
> 
> [snip]
> 
> At first glance, nothing special, but there is a problem, a big problem,
> that I hadn't noticed :(

I think I see where this is going.

> Can you see it too? Well done.
> 
> 
> I also program in C, so perhaps that's where the confusion comes from.

Now I'm sure of it!

> But I'll explain it anyway.
> 
> 
> The annoying part is this one :
> 
>     #if ( k > n-k )
>       #local k = n - k;
>     #end
> 
> ... and so, the "#local k =" led me to believe I was using a local
> variable. Well, not at all. "k" represents the variable passed as a
> parameter and its value is modified in the part that called this macro.

Saw it coming!  This feature (not a bug!) has zapped some veterans,
including BE, Ive, and the author of colors.inc::CH2RGB().

The particulars are explained here:

https://wiki.povray.org/content/Reference:User_Defined_Macros#Returning_Values_Via_Parameters


Post a reply to this message

From: kurtz le pirate
Subject: Re: Macro and parameters
Date: 29 Nov 2025 04:05:40
Message: <692ab764$1@news.povray.org>
On 29/11/2025 02:05, Cousin Ricky wrote:
> 
> Saw it coming!  This feature (not a bug!) has zapped some veterans,
> including BE, Ive, and the author of colors.inc::CH2RGB().
> 

Effectively, it's not a bug, I agree, but it's confusing when you're 
switching between C and POV. My code wasn't working, and I had a lot of 
trouble figuring out why.

Now I will pay much more attention to this situation.





-- 
kurtz le pirate
compagnie de la banquise


Post a reply to this message

From: Bald Eagle
Subject: Re: Macro and parameters
Date: 29 Nov 2025 10:10:00
Message: <web.692b0c9d8e84ac711f9dae3025979125@news.povray.org>
Cousin Ricky <ric### [at] yahoocom> wrote:

> Saw it coming!  This feature (not a bug!) has zapped some veterans,
> including BE, Ive,

It's an easy thing to forget about.
There has been some discussion about "wrapping" the feature, using a flag/switch
for enabling/protecting against the feature, or issuing a warning "argument
valued altered".

> and the author of colors.inc::CH2RGB().

Story time!  :)


> The particulars are explained here:
>
>
https://wiki.povray.org/content/Reference:User_Defined_Macros#Returning_Values_Via_Parameters

Yes, I had that tab open in my browser just in case.
You beat me to it.
I would like that whole section to moved the the top of the page,

"This mistake is more likely to be made with float identifiers versus float
expressions. Consider these examples.

#declare Value=5.0;
MyMacro(Value)     //MyMacro can change the value of Value but...
MyMacro(+Value)    //This version and the rest are not lone
MyMacro(Value+0.0) // identifiers. They are float expressions
MyMacro(Value*1.0) // which cannot be changed.
Although all four invocations of MyMacro are passed the value 5.0, only the
first may modify the value of the identifier."

and for the example preceding to be a little clearer.  The box {} example is a
bit too strange to lead in with.

The numerical examples are good, but a full-fledged .pov file with extended
commentary and output would be best - either as a downloadable file or just
cut-n-paste SDL right there on the wiki page.

- BE


Post a reply to this message

From: Cousin Ricky
Subject: Re: Macro and parameters
Date: 29 Nov 2025 10:57:56
Message: <692b1804$1@news.povray.org>
On 2025-11-29 11:09 (-4), Bald Eagle wrote:
> Cousin Ricky <ric### [at] yahoocom> wrote:
> 
>> and the author of colors.inc::CH2RGB().
> 
> Story time!  :)

https://news.povray.org/povray.general/thread/%3Cweb.4fb9a70082db4bf885de7b680%40news.povray.org%3E/


Post a reply to this message

From: Cousin Ricky
Subject: Re: Macro and parameters
Date: 30 Nov 2025 13:54:52
Message: <692c92fc$1@news.povray.org>
On 2025-11-29 05:05 (-4), kurtz le pirate wrote:
> 
> Effectively, it's not a bug, I agree, but it's confusing when you're
> switching between C and POV. My code wasn't working, and I had a lot of
> trouble figuring out why.

Kinda like switching from English to Spanish, and saying "vacunar la
carpeta" when you want to say "vacuum the carpet"; or saying "Estoy
embarazada" when you want to say "I'm embarrassed"; and then having a
lot of trouble figuring out why the locals are looking at you strange.


Post a reply to this message

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