POV-Ray : Newsgroups : povray.advanced-users : user defined function : Re: user defined function Server Time
28 Jul 2024 12:32:22 EDT (-0400)
  Re: user defined function  
From: Warp
Date: 11 Jan 2006 19:03:53
Message: <43c59ce6@news.povray.org>
Leo80s <nomail@nomail> wrote:
> Can you explain exactly in what macros and user defined function are
> different, lease?

  Macros are a lot more generic entities than functions. While a macro
can be used as if it was a function (except in certain cases), it can be
used for a lot more. For example, you can make a macro which creates an
object (something which a user-defined function cannot do).
  In a way, you can think about a macro as if it was a subroutine call
in a programming language.

  A user-defined function resembles a lot more a mathematical function:
It takes numerical parameters and it returns a numerical value (based on
mathematical operations performed on these input values), and it basically
cannot do anything else. You cannot use a user-defined function for example
to create an object.

  For example:

#macro CreateSphere(Location, Radius)
  sphere { Location, Radius }
#end

  This is not a mathematical function. It doesn't even return a numerical
value. It just creates a sphere. Macros don't really have a return value
per se.
  Macros work almost like substitution macros in C: It's almost as if
the macro call was substituted by the contents of the macro.

  You can't do that with user-defined functions. These can only be used
for calculating a mathematical operation, and the return value of such
function is the result of the operation in question.

  You can often simulate a function with a macro. For example:

#macro Length(A, B, C)
  sqrt(A*A+B*B+C*C)
#end

  This will work a lot like a function. You can call it like Length(1,2,3)
(you can think about it as if the call in question is substituted by the
contents of the macro).

  That is basically the equivalent of:

#define Length = function(A,B,C) { sqrt(A*A+B*B+C*C) };

  Then what is the difference? Why have user-defined functions at all
since macros can be used for the same thing?

  For one, user-defined functions are a whole lot faster. If you need to
call such a function millions of times, it's a whole lot faster to do it
with a user-defined function than with a macro.

  Secondly, user-defined functions can be used in some render-time
features, more specifically isosurfaces and parametric surfaces, while
macros cannot (macros are purely parse-time entities and they simply
don't exist at render time).

-- 
                                                          - Warp


Post a reply to this message

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