Jump to content

Recursive VEX functions


Trancor

Recommended Posts

Hey guys, I've been trying to find a way of making a recursive function in VEX.
I'm trying to aggregate infromation from points and build an return array, but can't get any recursive funtions to work.
For example, when ever I try running something like-

void printer(int count){
    count+=1;
    printf("\n %c", count);
    if(count < 10){
        printer(count);
    }
}

printer(1);

It throws the error-
"Call to undifiend function 'printer'"

I know I can go the messy route with while loops and maintaining variables by adding and subtracting elements and having a result variable...  but this is needlessly insane to try to do a basic recursive function.
I've tried giving pointer variables, and it seems like vex doesn't want to accept standard C pointer code. Or at least doesn't like * in the input variables of a void definition.

Has anyone had success with this and might be able to point me in a useful direction for vex code?
(No, this can't be done with nodes or a python node for the real node I'm working on)

Thanks in advance!

-Kevin

Edited by Trancor
Link to comment
Share on other sites

Guest mantragora

You wound need to define it in Outer code to make it work. Don't ask me how to do it in any inline/snippet/etc. node, I failed to make it work, some syntax problems. But if you create new node definition, you have Inner and Outer tabs and Inner is where you execute code, Outher is where you define helper functions before the main body of the VEX code.

Link to comment
Share on other sites

Guest mantragora

... But if you create new node definition...

VOP node definition :)

The Outer is also place where you Include files. You cannot do this in Inner part.

Link to comment
Share on other sites

Interesting, I never knew what this Outer Code option was meant for.  But I'm unsure how to use this vex code without it being on snippet.  Which its throwing an error, probably the same one you are talking about.
Unexpected "[" on line 22.

Course, I'm just putting "printer" into the Outer Code parm.
I tried putting it into a header file and loading the header file from the file, but it also throws that same error.
So I'm probably trying to use this Outer Code option wrong.
Can you help any further?

Are you using this Outer Code method on a VEX node itself?  Writing out a VCC compiler command?
Because when ever I try to load up VEX that have nested headers, it can't find the header.  Which is also what was happening when I tried using the Outer Code option on the snippet with any additional headers I'm trying to include.

Sorry for my ignorance on this topic.

Link to comment
Share on other sites

Guest mantragora

You won't like it, but actually recursion is not possible in VEX, sorry. I write to much HDK :D
You would need to put it in something like SolverSOP to make it work. Otherwise you can't accumulate it.

But you can go Python way and use InlineCPP if you need more speed. In Python you can do recursion.

 

Edited by mantragora
  • Like 1
Link to comment
Share on other sites

I'll look into shader calls some, but yeah, dunno if this will help much.

VEX might or might not be Turing Complete, but who knows, might need some special syntax, ha.

I've written up my script through VEX since it runs VEX commands native to houdini.  To jump between python nodes and back would be cumber some and slow things down.  I'd need a ton more nodes to pass the data around or use python nodes just to generate attributes and process data to pass back to vex.  Not exactly what I'm looking for when it would take a simple 5-6 line void to do the process.

I've gone a different route now anyway, whiles loops and maintaining variables.  I'm not a fan, but it works.

Thanks for the info Mantragora, I'll have to look into Outer Code more in the future.

Link to comment
Share on other sites

  • 3 weeks later...
  • 3 months later...

One can always simulate function recursion using an array-based stack

 

Can you please show a simple example on how to do this Edward? Is it gonna involve a for loop where you are iterating through the stack and accumulate the result in a variable stored outside?

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...