POV-Ray : Newsgroups : povray.advanced-users : user defined function Server Time
28 Jul 2024 16:30:09 EDT (-0400)
  user defined function (Message 11 to 14 of 14)  
<<< Previous 10 Messages Goto Initial 10 Messages
From: Tor Olav Kristensen
Subject: Re: user defined function
Date: 11 Jan 2006 20:09:38
Message: <43c5ac52@news.povray.org>
Warp wrote:
...
>   If the macro contains control flow commands, you have to use a small
> trick to put a "return value". It works for example like this:
> 
> #macro Func(A)
>   #if(A < 0)
>     #local Result = -A;
>   #else
>     #local Result = A;
>   #end
>   A
> #end
> 
>   The value of the last line of this macro is what is ultimately used as
> the "substitution" of the macro call, which is practice is its "return
> value".

Isn't this what you meant to write Warp ?

#macro Func(A)
   #if(A < 0)
     #local Result = -A;
   #else
     #local Result = A;
   #end
   Result
#end


-- 
Tor Olav
http://subcube.com


Post a reply to this message

From: Warp
Subject: Re: user defined function
Date: 12 Jan 2006 03:21:08
Message: <43c61173@news.povray.org>
Tor Olav Kristensen <tor### [at] toberemovedgmailcom> wrote:
> Isn't this what you meant to write Warp ?

  Yes.

-- 
                                                          - Warp


Post a reply to this message

From: Leo80s
Subject: Re: user defined function
Date: 12 Jan 2006 07:25:01
Message: <web.43c649b0ca20965f72071f7b0@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> Tor Olav Kristensen <tor### [at] toberemovedgmailcom> wrote:
> > Isn't this what you meant to write Warp ?
>
>   Yes.
>
> --
>                                                           - Warp

Thanks Warp and Olav...
very gentle.

Leo


Post a reply to this message

From: Paolo Gibellini
Subject: Re: user defined function
Date: 18 Jan 2006 04:14:37
Message: <43ce06fd@news.povray.org>
This is an excellent synthesis, Warp!
Can you add it to the POV-Wiki? I think it could be useful for beginners.
;-)
Paolo

"Warp" 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

<<< Previous 10 Messages Goto Initial 10 Messages

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