[Next] [Up] [Previous] [Contents] [Index]
Next: 6. Strings, Arrays and Up: NannyMUD LPC Previous: 4. Flow Control


5. Functions and Program Structure

LPC functions work in the same manner as functions in other languages; they make it possible to split a problem into many small parts, write code once, use others code and help make the code readable, etc.

5.1 Efuns, Sfuns and Lfuns

There are several classes of functions available to an LPC object:

5.2 Functions without type

Functions can be declared without specifying a type, unless the #pragma strict_types is in effect, in which case all functions must have types. Declared without types, the function arguments can be declared without types, too, and the function will accept that the actual number of arguments passed differs from the number of arguments declared. If called with too many arguments, the extras are ignored. If called with too few, the missing ones default to zero.

Functions declared without types can be called from any other object, they can be called from inheriting objects as well, and they can be overloaded10.

5.3 Function types

A function can have the same types as a variable, and then one more: void. A function of type void cannot return any value; doing so is considered an error at load-time.

If the type of the function is specified, the type of the arguments must also be given. If the types of the arguments are specified, the type-checking usually given by #pragma strict_types will be performed on the statements of the function. Also, the number of arguments passed to the function must be the same as the number declared.

There is a set of type modifiers that can be used on the functions type: static, varargs, public, private

5.4 Function Prototyping

When using the #pragma strict_types, functions must be declared before they are used. This will in many cases force functions to be declared using prototypes. This means that the functions type, as well as what parameters it takes and their type, is specified, but the body of the function (with a repetition of the type etc.) appears further on in the file.

5.5 Functions Returning Strings, Arrays or Mappings

A function can be exited at any point by adding a return expr; statement at the desired point of the code. The value of 'expr' is then the value returned from the function to the calling code.

A function returning a string, mapping or an array does so by returning a reference to it, rather than a copy. This can be used to keep shared strings/arrays/mappings, thus saving some memory. If you'd rather want a copy, you will have to force it. This is easiest done by returning the sum of the string/array/mapping and an empty string/array/mapping.

5.6 Calling Non-Existant Functions

In LPC, calling a non-existing function in another object returns a zero. This is a feature. On the other hand, calling a non-existing function locally (in the same object) gives a run-time error. This is, also, a feature.

5.7 Scope Rules

The scope of functions and global variables is from their point of definition to the end of the file. The scope of formal parameters (those declared as part of a function declaration) is the whole of the function.

Local variables can be declared at the beginning of any block, and their scope is from their point of declaration to the end of the function, not just the end of the block where they were declared. This can be considered an unwanted behaviour. Local variables hide any global variables with the same name.

5.8 Block Structure

Functions cannot be declared within functions in LPC, but within the functions, blocks can be declared within blocks. Each block can begin by declaring variables, but local variables can only be declared once.

5.9 Recursion

Functions in LPC can be called recursively. The driver will allow a certain depth of the recursion before it stops the code. Circular recursive calls will also be detected.

5.10 The Preprocessor

The LPC preprocessor works like C's, almost. Somewhat more details:

There are some things that works in the standard C preprocessor but that cannot be used in the LPC preprocessor:

5.11 Comments in LPC

LPC allows comments over whole blocks by enclosing it in `/*' and `*/', just like in C. It also allows for comments to the end of line using the C++-style `//'.


... overloaded10
See section 7.2.
... 'shadowing'11
See section 8.1 on shadows.
... /include12
Or rather, in a list of places defined by the mudlib. This list is usually determined by /obj/master.c.

[Next] [Up] [Previous] [Contents] [Index]
Next: 6. Strings, Arrays and Up: NannyMUD LPC Previous: 4. Flow Control
Mats Henrik Carlberg