POV-Ray : Newsgroups : povray.advanced-users : user defined function Server Time
28 Jul 2024 16:16:34 EDT (-0400)
  user defined function (Message 5 to 14 of 14)  
<<< Previous 4 Messages Goto Initial 10 Messages
From: Leo80s
Subject: Re: user defined function
Date: 11 Jan 2006 11:50:01
Message: <web.43c53669ca20965f443403d30@news.povray.org>
Warp <war### [at] tagpovrayorg> wrote:
> kurtz_le_pirate <kur### [at] yahoofr> wrote:
> > #macro myFunction(a,b,c)
> >     sqrt(a*a+b*b+c*c)
> > #end
>
>   There's a difference between macros and user-defined functions.
>
> --
>                                                           - Warp

Can you explain exactly in what macros and user defined function are
different, lease?
thanks Leo


Post a reply to this message

From: Leo80s
Subject: Re: user defined function
Date: 11 Jan 2006 12:40:00
Message: <web.43c54273ca20965f443403d30@news.povray.org>
Hello , I need an help...

I can't understand the way I could specify the value returned by a function
(or a macro?!)...isn't there something similar a return statement?!?!

for example, how can I write a function that take an integer as parameter
and give in output the value of this integer if it's positive and 0
otherwise???

Leo


Post a reply to this message

From: Mike Williams
Subject: Re: user defined function
Date: 11 Jan 2006 13:25:10
Message: <wT7frDAUxUxDFwYL@econym.demon.co.uk>
Wasn't it Leo80s who wrote:
>
>how can I write a function that take an integer as parameter
>and give in output the value of this integer if it's positive and 0
>otherwise???

#declare myFunction = function(a){select (a, 0, a)}


-- 
Mike Williams
Gentleman of Leisure


Post a reply to this message

From: kurtz le pirate
Subject: Re: user defined function
Date: 11 Jan 2006 13:47:16
Message: <kurtzlepirate-2E0094.19471611012006@news.povray.org>
In article <43c4fd96@news.povray.org>, Warp <war### [at] tagpovrayorg> 
wrote:

> kurtz_le_pirate <kur### [at] yahoofr> wrote:
> > #macro myFunction(a,b,c)
> >     sqrt(a*a+b*b+c*c)
> > #end
> 
>   There's a difference between macros and user-defined functions.

oooops, you'r right... read too quick, answer too quick :))
klp


Post a reply to this message

From: Warp
Subject: Re: user defined function
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

From: Warp
Subject: Re: user defined function
Date: 11 Jan 2006 19:08:59
Message: <43c59e1a@news.povray.org>
Leo80s <nomail@nomail> wrote:
> I can't understand the way I could specify the value returned by a function
> (or a macro?!)...isn't there something similar a return statement?!?!

  The return value of the function is directly the evaluated expression
inside it. You don't have to "return" it explicitly.
  If you write:

#declare Func = function(A) { A*A };

then the return value of Func will be its parameter squared.

  With macros things get slightly more complicated. If you have just
a simple mathematical expression as the body of the macro, that serves
as its "return value". For example:

#macro Func(A)
  A*A
#end

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

-- 
                                                          - Warp


Post a reply to this message

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 4 Messages Goto Initial 10 Messages

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