Defining procedures
 
 
 

Global procedures

Once you define a global procedure, you can call it anywhere: from any script file, within any function, or from the command line. The general form of a global procedure declaration is:

global proc return_type procedure_name ( arguments ) { 
	MEL_statements 
}

Return values

If you specify a return type for a procedure, then you must use a return operator somewhere in the procedure’s code block to return a value.

global proc float square(float $x) {
	return $x * $x;
}
square(5.0);
25

If you don’t specify a return type for a procedure (indicating that the procedure will not return a value), then you can only specify the return operator without a return value. Use of a return operator in this context serves as a function break.

// This does not work. 
global proc add5(int $x) {return $x+5;}; 
// Error: global proc cc(int $x) {return $x+5;}; // 
// Error: This procedure has no return value. //
// This works. 
global proc add5(int $x) {return;};" 

Examples

Here are some example procedure declarations:

global proc string sayHi() {
	return "Hello!\n");
}
global proc float square(float $x) {
	return $x * $x;
}
global proc int max(int $a, int $b) {
	if ($a > $b) {
		return $a;
	} else {
		return $b;
}
global proc msg() {
	print "This proc has no return value.\n";
}

Local procedures

If you leave the global keyword off the beginning of a procedure declaration, the procedure is local to the file in which it is defined.

// This is a local procedure
// that is only visible to the code in this file.
proc red5() {print("red5 standing by...\n");}

This is very useful for making “helper” procedures that do work for other procedures. You can expose just one or two global procedures, hiding the helper code from other people using your scripts.

You cannot define a local procedure in the Script Editor. They are only available in external script files.

NoteMEL does not allow you to forward reference locally scoped procedures. Locally scoped procedure definitions must appear before they are called. For example, in a file called noForwardRef.mel, define the local procedures before they are referenced.

proc myLocalProc() { print "In myLocalProc()\n" ; } proc anotherLocalProc() { print "In anotherLocalProc()\n" ; myLocalProc; } global proc noForwardRef() { print "Calling anotherLocalProc()\n" ; anotherLocalProc; }