Functions or Procedures? What’s the difference?

loocas | 3ds Max,maxscript,technical | Friday, April 25th, 2008

Functions or procedures banner

In general, mainly lower level, programming languages, you have access to both types of data handling blocks: 1) Functions and 2) Procedures. So what is the difference and what the hell am I talking about?

Well, let’s think of an example, you were assigned a task of, let’s say, manipulating thousands of objects in your scene, for some reason. To make it even more complex, you have to manipulate those thousands of objects in hundreds of different *.max files. Quite frankly, I certainly wouldn’t wish to do this manually, that’s out of the question. Whatever can be done for you, it’s not necessary for you to do and then you can fully concentrate on much more creative and fun tasks.

Back on topic. The way to approach this is, obviously, through a few lines of code. Preferrably, you’d write a function (general Max term for blocks) that’d loop through the *.max files and then through the entire scene in each of the files. There’re countless numbers of ways to structure your code, so I’ll just write two examples to demonstrate my point.

The first example will use a PROCEDURE (which is a more common use of “functions” in Max) to do the following: move each and every Box in the currently opened scene by a random number in X axis.

fn moveBoxes = (for o in $Box* do move o [(random -10 10),0,0])

This example demonstrates an obvious PROCEDURE. Let’s take a look at how this same example of moving all Boxes in the scene by a random number in X axis can be turned into a regular FUNCTION expression.
First, you have to realize the difference, which I haven’t mentioned yet, between PROCEDURES and FUNCTIONS. As the example above demonstrates, a simple procedure takes arguments and processes them inside
the procedure block, on the other hand, a function always RETURNS a certain value. This simple rule requires the scripter to design the code in a bit different way. Now, since “random()” already IS a function, therefore returns a random nuber of the given range, I’ll demonstrate the functionality on a different element of the command. I’ll ask a custom function that I’ve written, to return what elements should be randomly moved. So, let’s take a look:

I’ll first construct the function to return the array of objects to move:

fn whatObjects = (tmpArr = $Box*; return tmpArr)

Notice the use of the “return” command, which isn’t necessarily required in order for MAXScript to return a certain value, but it’s generally a good idea to put it in to explicitly differentiate functions from procedures as well as to avoid unexpected results.

Then I’ll have to ask for the objects to move in another line this way:

for o in whatObjects do move o [(random -10 10),0,0]

To sum it up, in this second example, at first I defined a function called “whatObjects” which RETURNS an array of objects in the scene, in this case it’ll return all objects whose name starts with a “Box”. Then, in a general param block I asked for the result of such a function in a loop block. So, basically, the principle is that FUNCTIONS ALLWAYS RETURN a value as opposed to PROCEDURES, that ALLWAYS PROCESS certain tasks. This is a very important difference as you can use functions to return, let’s say true or false upon which you can call procedures to process some data for you based on the output of the function.

One interesting note. In Maya, functions are actually called procedures by default and are constructed like so:

proc procedureName(<parameters>)

while in Python, functions and procedures don’t have any declaration name and are both accessible throu a “def” statement, like in this example:

def procedureOrFunctionName(<parameters>):

And for the sake of coherence, here’s the MAXScript syntax:

fn functionName <parameters> =

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress | Theme by Roy Tanck