Functions
As we've learned, functions are used to do a specific task and to reuse a specific block of code. On this page, we'll go through functions in better detail.
There are three types of functions in Powerlang: user functions, global functions and instance functions. User functions are functions that you define in your code - you control what the function does, what inputs and outputs it has. Instance functions, on the other hand, belong to objects and allow you to perform a predefined set of operations on them. Each kind of object has its own instance functions. Global functions behave similarly to user functions, however they are predefined by the language to do some kind of task, and they cannot be modified.
User functions help you organize common tasks or simplify a script if the same code is used over and over. User functions have:
A name,
A task they perform,
Optional inputs,
Optional outputs.
Let's return to an example from one of the previous pages, a user function that takes two number inputs, adds them together and outputs the result:
This example involves definining a user function with the name Sum and inputs num1, num2. The function then adds these numbers (num1 + num2
) and then "returns" (outputs) them (return num1 + num2
). After the function is defined, we call it while entering 20 and 49 as inputs. The function adds 20 and 49 together, and "returns" the result, which is 69. Then we use the result in the global print() function, which outputs something (in our case, 69) to the Output window.
Now let's take a look at instance functions. Instance functions belong to objects and do some kind of task related to an object. Examples of instance functions each object has are: :Destroy(), :GetChildren(), :Clone(), etc. More sophisticated object types have their own instance functions, for example, the Humanoid type has the :TakeDamage() function, which makes a character lose health.
Here is how one might clone a Part and color it blue:
Hopefully, now you understand functions. Let's move on to tables.
Last updated