Function Argument Passing

Arguments can be passed by reference, or value. The default behavior is to pass by reference. Pass by references indicates that the variables used in the function call are directly referenced from within the function. Pass by value means that only the value of the argument is passed to the function. In other words, the arguments are copied. In order to pass a function argument by value, the user needs to declare that function argument as local. This method allows users to selectively determine how to pass each function argument.

A function can be called with fewer arguments than specified in the definition; this is called a ``short-list''. When this situation occurs, RLaB  pads the argument list with extra undefined variables. Arguments can be ``skipped'' when calling a function. The ``skipped'' arguments are passed to the function as undefined variables. To ``skip'' an argument just leave it out of the argument list, but don't forget the commas:

> x = function ( a, b, c, d ) { a ? b ? c ? d ? }
	<user-function>
> x (1);	// short-list
        1
	UNDEFINED
	UNDEFINED
	UNDEFINED
> x(1,,2);	// skipped arguments
        1
	UNDEFINED
        2
	UNDEFINED

As far as RLaB  is concerned undefined variables do not exist. The function exist will return true (1) if its argument exists, and false (0) if its argument is undefined.

A function cannot be called with more arguments than specified in the function definition. If you attempt to do so, a run-time error will result.

Function argument classes and class-types are not specified during definition. When writing ``robust'' functions the author should take some care to check that the function argument(s) are of the correct class and type, if necessary. The documentation (comments) should clearly define the requisite argument types and the function return object. If the function documentation does not clearly state that the arguments will be modified during function execution, care should be exercised to avoid changing any of the function arguments. If necessary, the function arguments can be passed by value so that changes will not affect the caller's variables.

If you wish to write function(s) to serve as often used utilities or libraries, then care should be taken to declare all variables (other than function arguments or built-in functions) as local. Declaring all function variables as local will prevent accidental destruction of user's global variables.

RLaB  has a special built-in function (fvscope) that analyzes user-functions, and makes a report describing which variables are local, arguments, global, or file-static. fvscope can be very helpful when writing your first function(s) to help you understand how RLaB  resolves variable references.