Notifications
Clear all

[Closed] Functions inside functions

I was working today on creating a function to force naming conventions, this is the main function. The function required several other functions inside of it that would be very specific to the needs of this main function, I’ll call them sub-functions. I didn’t want to start cluttering my script with these random functions that were specific to this one function, so wondered if it would be possible to place the sub functions inside of the main function. I played around with the idea and now have a working version of the main function.

Here is a basic example of what I am talking about..

    fn mainFunction ar =
    (
    	fn subFunction obj =
    	(
    		print obj
    	)
    	
    	for x in ar do subFunction x
    )
    
    mainFunction #(2,3,4,2,35,65)
    
Now, I am doing this just for organizational purposes.  I figure it does no harm as it is in the scope of the main function and the sub function still requires variables to be passed to it.  This, for example, would not work..

     fn mainFunction ar =
     (
     	fn subFunction = -- obj has been removed from function definition
     	(
     		for x in ar do subFunction x -- function is calling parameter being passed to mainFunction
     	)
     )
     
     mainFunction #(2,3,4,2,35,65)
     
So, it still needs to be formatted as if it resides outside of the main function.  As soon as I have a use for it outside of the function I can bring it outside and not have to change any code.

    fn subFunction obj =
    (
    	print obj
    )
    
    fn mainFunction ar =
    (
    	for x in ar do subFunction x
    )
    
    mainFunction #(2,3,4,2,35,65)
     
Is this sub function technique legal to use?  Can anybody seeing it causing any problems? I haven't had any issues with it yet, I am just curious.
7 Replies
 JHN

Why not use structs to cluster functions, if it’s only for organizational purposes…
There’s no reason not to use subfunctions, but as you say, you can only use it for that specific function, they’re the “private” functions.

I would stick with structs and group similar functions in the struct, similar to a class in some other programming languages. Have a struct for poly operations, text functions etc.

I really try to build everything in structs nowadays, feels more robust and organised, but thats my humble opinion

-Johan

Yep, you’re both correct. It all comes down to design.

If the sub-function only provides a service to it’s parent function, and you can’t see any other needs for that functionality out side of the parent function, then I would use the sub-function approach.

But, it you’ve written a neat function that you might like to use again, add it into a struct or other area of the code…

ps – I really like the struct idea

Shane

 PEN

Structs are the best way to go but I didn’t even know you could put a function in a function. I don’t think that I have ever seen that in any lanuage. Is it legal? I know it works now but is this one of those times where it is a over sight and in the next version it will not work?

 JHN

I don’t think it’s illegal or an oversight, it’s just the way you can use it… PHP allows this, javascript & actionscript as well. So I think there’s no problem with it, only I never use it in mxs… I use structs

What would be really cool though is the concattenating (hope I spell this right) of functions.

obj.dotransform.elastic.easeIn()

instead of

easeIn ( elastic (dotransform obj ) )

But I don’t see that happening for maxscript…

-Johan

I think it all comes down to design.

If you have a section of code that is repeated inside a function, that is doing the same job, logic would dictate that encapsulating it within it’s own function is probably a good idea. But what if that function provides no other functionality to any other part of the script or library?

Putting it inside the parent function provides you with the ability to remove some of the clutter and prevent the function from been mis-used.

I tend to only use them when I don’t need a struct (as the code is only been used within the current context of the script – ie not a library) and it meets the above needs. This is not very often mind.

It’s one of these decisions which you have to make for yourself based on what you want to get out of the code.

ed: Think of functions within functions as “private” functions or methods. I also think the point is, if you can’t see a reason to use them, then don’t.

Shane

 PEN

It is interesting as I’m getting back to python I find that I can do nested functions there as well. I don’t see a lot of use for them in max script for the reasons that you point out. Having private functions sounds great but not needed 99% of the time.

 JHN

Private functions become very usefull for objects and securing a data transactions (I’m reading a C++ book right now :)).
You’ll want this sort of functions to make sure a transaction doesn’t get interrupted or an dataobject can be corrupted by letting everything access everything all of the time. But that’s not the sort of thing you’ll use maxscript for. PHP can come closer to using this sort of functionality and python too maybe, but haven’t looked into it (though really wanting too…).

-Johan