POV-Ray : Newsgroups : povray.newusers : macro argument help : Re: macro argument help Server Time
2 Nov 2024 09:19:10 EDT (-0400)
  Re: macro argument help  
From: Stephen
Date: 28 May 2004 16:21:45
Message: <40b79f59$1@news.povray.org>
I learned this the hard way after lots of testing. And I discovered the
trick for ...

>> This has a curious side-effect that if you make a call like
Foo((identifier))

where I used Foo( +param) instead.  I feel like an expert now (although soon
to be challenged yet again).
So it really is in the documentation, but sometimes a quick nudge in the
right direction helps a lot.

Thanks for the clear explanation.

Stephen


"Warp" <war### [at] tagpovrayorg> wrote in message
news:40b72cf8@news.povray.org...
> Kaveh Bazargan <kav### [at] delete_thisfocalimagecom> wrote:
> > I think there is a fundamental point here about macro arguments which I
> > need to learn. Why is segments zero in the line marked "//the back"?
> > Any pointers welcome.
>
>   I don't know how much you know about programming, but #macros take their
> parameters by reference.
>   That means that the macro does not get a copy of the thing you give it
> as a parameter, but a reference, that is, a kind of "pointer" which points
> to the original thing.
>   This means that if you give it an identifier, any modification the macro
> makes to the parameter (even if it's done with #local) will be made to
> the original identifier.
>   If you want to avoid a macro modifying its parameter, make a copy of it.
> That is, instead of:
>
> #macro Foo(param)
>   #while(param)
>     ...
>     #local param = param-1;
>   #end
> #end
>
> do this:
>
> #macro Foo(param)
>   #local index = param;
>   #while(index)
>     #local index = index-1;
>   #end
> #end
>
>   (Btw, if you give an expression as parameter to a macro, a temporary
> variable will be created and the reference will point to that, and
> the temporary variable is destroyed after the macro call is over. That's
> why it's ok to use expressions as parameters, eg Foo(a+b-1).
>   This has a curious side-effect that if you make a call like
Foo((identifier))
> that's an expression and not the identifier directly and thus the value of
> the identifier will be copied to a temporary variable and the macro will
not
> modify the original identifier. However, it's better to make the macro
> itself safe and not trust that the caller takes care.)
>
> -- 
> plane{-x+y,-1pigment{bozo color_map{[0rgb x][1rgb x+y]}turbulence 1}}
> sphere{0,2pigment{rgbt 1}interior{media{emission 1density{spherical
> density_map{[0rgb 0][.5rgb<1,.5>][1rgb 1]}turbulence.9}}}scale
> <1,1,3>hollow}text{ttf"timrom""Warp".1,0translate<-1,-.1,2>}//  - Warp -


Post a reply to this message

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