Fandom

Scratchpad

Basics of Functions

215,833pages on
this wiki
Add New Page
Discuss this page0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Print Function

The following function will print 'hello world' whenever it is called.

void HelloWorld()
{
  print "Hello World\n"
}

Because DXL doesn't require parenthesis if there is zero or one arguments, after this function has been declared it can be called by simply typing the name of the function in a line. Using a function the Hello World program could be written as follows.

void HelloWorld()
{
  print "Hello World\n"
}
HelloWorld

Functions with a single argument also don't require parenthesis. This can be demonstrated with a simple modification of our HelloWorld program.

void HelloWorld(string s)
{
  print "Hello World " s "\n"
}
HelloWorld "Wide Web"

This simple script will print the string "Hello World Wide Web". This also exemplifies another important feature of DXL. When next to a string, the space character is used as a concatenation operator. In this way the three strings are added together, and printed.

A bit more useful function

The previous function really would have been easier to accomplish with a simple print line. What I will attempt to do here is to return the factorial of a supplied number.

int Factorial(int n)
{
  int fact = 1
  while(n>1)
  {
    fact *= n
    n--
  }
  return fact
}

This function demonstrates a few important topics. The first of which is return type. The 'int' that precedes the name of the function tells the compiler that this function will return an integer. After calculating the factorial, this function should return the factorial of the given number. We could access this in the following way.

int hundredFactorial = Factorial(100)
print hundredFactorial ""

Note the empty string at the end of the print line. We have to convert the integer to a string before we pass it the print function, or it will crash. See Type Conversion for more information on this action.

Also on Fandom

Random wikia