Functions

Functions are pieces of code that perform a smaller task in the overall program. Functions are executed when they are called by other code. Some functions perform tasks and return values to be used in calling code, while others just perform tasks and don't return anything.

Functions in VC have the following general syntax:

ReturnType FunctionName(Type argument_name, Type argument_name, ...)
{
    // code
}

Where ReturnType is void, int or string, each Type is either int or string, and code is a set of statements to be executed.

First off, there is one function in VC that is special, called "AutoExec". It is automatically executed as the first thing in your VC, and it looks like this

void AutoExec()
{
    // code to run
}

You could write every piece of code in AutoExec, but that is definitely not recommended. Instead, split up tasks into smaller functions. Here is another function that can be used inside AutoExec. This one takes two arguments, a string and an integer.

void ShopMessage(string name, int price)
{
    Text("The " + name + " is " + str(price) + " GP.");
}

To call a function, simply write that function's name, followed by a value for each of its arguments inside the brackets:

ShopMessage("Potion", 100);

In the above examples, the functions have a return type of void, but as mentioned earlier, there are int and string functions too. Here are some examples:

int GetMagicNumber()
{
    return 27;
}

string GetName()
{
    return "Bob";
}

void AutoExec()
{
    int num = GetMagicNumber();
    string name = GetName();
}

The return statement is a special statement used in functions, that will give the value back to the calling code and exit the function. In void functions, it can't return a value, but it can be used to exit a function early:

void Funky()
{
    return;
    Log("This will never be printed because of the return; above");
}

Here is a function that adds two numbers, logs the result and then returns the result to calling code:

int Add(int a, int b)
{
    int c = a + b;
    Log(str(a) + " + " + str(b) + " = " + str(c));
    return c;
}

One special thing to notice about the above code is the declaration of a local variable, an int named c. Local variables are available inside a function, and they cease to exist after a return. So things like loop counters and temporary calculations are recommended to be local, whereas variables that you use in several spots should be global (outside of functions).

For advanced function usage, a function can call itself. When a function calls itself (or a function calls another function that calls some other function that eventually calls the original function ), it is recursive. Each call of a function has its own unique local variables and arguments, which are kept on a stack. One popular example of recursive functions is the factorial operator n!, which is the same as n * (n-1) * (n-2) ... * 2 * 1. This can be calculated recursively, if you want.

// An example of recursion using factorials.
// Factorial(n) == n * (n-1) * (n-2) * ... * 3 * 2 * 1,
// Where n is a natural number, also Factorial(0) == 1.
int Factorial (int n)
{
	if (n <= 0)
	{
		return 1;
	}
	else
	{
		return n * Factorial (n - 1);
	}
}

Make sure not to go too deep into a recursion, because eventually, you will get a stack overflow. Exercise caution.

For those of you coming from a C background, VC does not support prototypes. Do not use them. Indeed, prototypes are not necessary at all. All function names are known to all other functions, so there is no need for something like this:

// Syntax error. Expects a brace {. VC doesn't have prototypes!
int Function(int a, int b);
// ...
int MyFunc(int arg1, int arg2)
{
	// Code
}

As mentioned earlier, the only arguments passable to a function are integers and strings. The only permissible return values are int, string, or void. This is sometimes an inconvenience, meaning arrays and structures can't be passed directly, and there is no pass-by-reference for primitve types. Instead, indices to the respective arrays must be passed. But you get used to it.

As of Verge 3.1, you can declare functions that may take any number of arguments. To utilize this functionality, see the Variadic Functions Section

Talkback

There are no talkbacks on this documentation page yet. Post the first?

Post a new comment?

Doc Nav

Your docs
View All Docs

If you log in, you can edit the documentation, or create your own documents and tutorials!

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.