Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.

Functions

Austin Lehman edited this page Oct 23, 2022 · 1 revision

Functions in Aussom reside within the class definition. That means in order to call a function, you need to reference the object and the function call. Let's start with function definitions.

Access Modifiers:

Below is an example of a basic public function definition. Notice the access modifier at the beginning is public meaning it can be called from outside of it's class. In this particular example we name the function functOne and the function expects 3 arguments when called. (arg1, arg2, and arg3)

public functOne(arg1, arg2, arg3) {
	// ...
}

Next is an example of a private function. Basically the only difference in the function definition from the previous example is the access modifier is private instead of public. This means that this function can only be called by other functions within the class. It cannot be called from outside of the class.

private functTwo(arg1, arg2, arg3) {
	// ...
}

To further illustrate the difference, consider the two function definitions below. One is public and the other is private. If we want to call functTwo, we need to call it from one of the other functions within the myObj class. We could potentially do this in functOne if we like. See the example below for details.

// Class definition with one public and one private function.
class myObj {
	public functOne(arg1, arg2, arg3) {
		// ...
	}

	private functTwo(arg1, arg2, arg3) {
		// ...
	}
}

// From another function. (Possibly main)
mobj = new myObj();		// Instantiate a new myObj instance.
mobj.functOne(1, 2, 3);		// Call our public function with 3 arguments.
mobj.functTwo(1, 2, 3);		// Runtime error! This function is private and not
				// accessible.

Note: If an access modifier is not present, the function is assumed to be private. Also, you cannot name two functions with the same name. If this occurs, the second function definition will overwrite the first.

Arguments:

Aussom offers quite a bit of versatility when it comes to function definition arguments. Let's explore the various function argument scenarios.

Here is a simple case. The function definition has 3 arguments. (arg1, arg2, and arg3) If we wanted to call functOne, we must supply all three arguments. The arguments can be any data type. See example below for details.

public functOne(arg1, arg2, arg3) {
	// ...
}

myObj.functOne(1, 2, 3);		// Valid, all 3 arguments supplied.
myObj.functOne("hi", true, 1.2345);	// Valid, all 3 arguments supplied.
myObj.functOne("wrong");		// Invalid! Function is expecting 3 arguments.

You can also use default arguments, which can be optionally specified when called. Any default arguments in the function definition must be added after any required arguments. When calling a function with default arguments, the default ones do not need to be specified when calling. See examples below.

// Definitions
public functOne(arg1, arg2 = true, arg3 = null) {
	// ...
}

public functTwo(arg1 = "", arg2 = 10) {
	// ...
}

public functThree(arg1 = "", arg2) {		// Syntax error! Default arguments must
	// ...					// go after required arguments.
}

// Calling our functions.
myObj.functOne("test", false, 12345);		// Valid, all arguments provided.
myObj.functOne("test");				// Valid, default arguments in the definition
						// will be used when function is called.
myObj.functOne();				// Invalid, the first argument is required!
myObj.functTwo();				// Valid, function definition has no
						// required arguments, defaults are used.
myObj.functTwo("Tyler Durden", 0);		// Valid, will use supplied arguments.
myObj.functTwo("abcde");			// Valid, arg2 will be set to the default 10.

The etcetera operator (...) can be used to allow any number of arguments to be passed to a function. It must go after any other arguments.

// Definitions
public functOne(...) {
	// ...
}

public functTwo(arg1, arg2 = false, ...) {
	// ...
}

public functThree(..., arg1, arg2) {		// Syntax error! Etcetera operator must go
	// ...					// at the end of argument list.
}

// Calling our functions.
myObj.functOne();				// Valid, any number of arguments are valid.
myObj.functOne("first", "rule", "about");	// Valid, any number of arguments are valid.
myObj.functTwo();				// Invalid! The first argument is required.
myObj.functTwo(true);				// Valid, only the one argument is required.
myObj.functTwo(true, false, "first", "rule");	// Valid, we are providing the first required
						// argument, the second, and any number of
						// extra arguments.

To use the etcetera operator within a function definition, we use the 'etc' variable. This variable is created when the function is called. It is a list that holds all the values provided. It can be used like any other list. We have not gotten to loops yet, but we could use 'etc' with a loop just like any other list.

public functTwo(arg1, arg2 = false, ...) {
	len = #etc;				// Get the number of items in the etc list.
	itemOne = etc[0];			// Get the first element in the etc list.
}

Note: There are functions that have an 'extern' modifier. We will go over these in the extern class section.

Function definitions may optionally specify a required data type. This is especially useful when writing extern functions. This means that you may only provide the required data type to a function with it's data type specified. Here are a few examples.

public functOne(int num, string str = "") { }
public functTwo(bool isTrue = false, double dbl) { }
public functThree(list Lst, map Mp) { }
public functFour(object Obj) { }

But wait, isn't Aussom a loosely typed language, why do we specify data types? In some circumstances, the library writer does not want to check every variable passed to a function to ensure it's type. This is especially useful to authors of external modules written in Java. By specifying the data type in the function definition, the parser will mandate that that data type is passed. So basically this shifts the ownership to the person using the function to ensure they are passing the correct data type.

Clone this wiki locally