POV-Ray : Newsgroups : povray.newusers : Returning values from macros : Re: Returning values from macros Server Time
7 May 2024 06:52:37 EDT (-0400)
  Re: Returning values from macros  
From: clipka
Date: 31 May 2016 12:32:34
Message: <574dbca2$1@news.povray.org>
Am 30.05.2016 um 15:40 schrieb Bald Eagle:

> Could someone please post a small, simple macro that results in a scalar, a
> vector, and an array getting passed "out" to the global SDL world?

Up to (and including) POV-Ray 3.7.0, you could use the approach
mentioned by Cousin Ricky:

    // the macro;
    // note that this macro does not actually "return" anything,
    // but has the side effect of redefining whatever variables
    // are passed as parameters
    #macro MyMacro (SomeScalar, SomeVector, SomeArray)
      // overwrite the parameters' values
      #declare SomeScalar = 4711;
      #declare SomeVector = <1,2,3>;
      #declare SomeArray = array[12];
      #local I = 0;
      #while (I < 12)
        #declare SomeArray[I] = ...;
        #local I = I + 1;
      #end
    #end

    // initialize the variables;
    // note that the types do not have to match,
    // but the variables need to be defined,
    // for rather obscure technical reasons:
    #declare MyScalar = false;
    #declare MyVector = false;
    #declare MyArray  = false;

    // call the macro, which will set the variables
    // to new values:
    MyMacro(MyScalar, MyVector, MyArray);


The current POV-Ray 3.7.1-alpha provides an alternative by making use of
a new syntax extension dubbed "tuple-style assigment":

    // the macro;
    // note that this does not take any parameters,
    // and instead "returns" the right-hand side for a
    // tuple-style assignment
    #macro MyMacro()
      #local SomeScalar = 4711;
      #local SomeVector = <1,2,3>;
      #local SomeArray = array[12];
      #local I = 0;
      #while (I < 12)
        #declare SomeArray[I] = ...;
        #local I = I + 1;
      #end
      // return all the values
      (SomeScalar, SomeVector, SomeArray)
    #end

    // invoke the macro to generate the right-hand side
    // for a tuple-style assignment;
    // note that the variables do _not_ have to be
    // defined beforehand:
    #declare (MyScalar, MyVector, MyArray) = MyMacro();

(It should be noted that this syntax extension does /not/ really add
tuple support to the SDL; all it does is provide a way to bundle
multiple assignments in such a way that the values for the entire batch
can be generated by a single macro invocation.)


Post a reply to this message

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