User Defined Functions

As with the C language, you can also create functions with NC.

The model() and control() functions are special cases of a user defined function. When model() exits, NEC-2 or NEC-4 called to process the "card deck" that has been generated. For optimization runs, the control() function is used to run multiple passes of the model() function, and be able to inspect the results from NEC after each pass through NEC.

An arbitrary user definable function behaves as any system call in NC, such as the trigonometric sin() function, and the user defined functions are called the same way the system functions are called.

Just as in a model() function, a user defined function can generate NEC "cards" by calling the system wire() or line() functions from the user defined function. Although it can generate NEC cards, unlike the model() function, a user defined function does not immediately ask NEC-2 to process the cards when the function exits. These cards are accumulated to the existing card deck and are only processed when the model() function itself exits.

A user defined function can call other user defined functions. A user defined function can also call itself recursively.

A user defined function can return a value to the function that calls it.


Defining a User Defined Function

A function is defined by prepending a function header to an NC block. Each function header must start with a type of the function. A function can return a value that is an int type, a real (or float) type or an element type. A function that returns no value is declared using a void type.

Each function has a function name or identifier that is followed an argument list within parentheses. Even when there are no function argument, an empty argument list has to be included.

Examples of functions are:

fnEx

The return keyword causes the function to exit and return a value. If the function is declared as a void function that does not return a value, a simple return (without a value) is used to exit a function. Please note that you cannot return an element value from an int function and vice versa. You also cannot return a value from a void function. E.g., the following will generate a compiling error message:

badFn

Returning a int value in a real function (and vice versa) is allowed. An int-to-real or real-to-int conversion takes place when that happens.

A user defined function can call system functions and other user defined functions. A function can call itself recursively, as seen in the factorial example above.

Other than a void function, all functions must end with a return statement. An implicit return statement is generated at the end of a void function if a return statement does not exist. As shown in the factorial example above, multiple return statements is allowed from a function.

A function that returns a real or an int value can be used in any place that a real or int variable is used. Similarly, a function that returns an element can be used in place of any variable of element type. For example, an element function can be assigned to an element variable, or included directly as an argument in a voltageFeed or currentFeed system call as shown below.

ex2

As seen in the above example, you can use a function before defining it (e.g., forward referencing is allowed).