UntitledGoose Posted December 26, 2020 Share Posted December 26, 2020 Introduction Functions are little doodads in Skript that hold snippets of code, and that function can be executed to run that code. Functions also take parameters, and can return values. You’ll learn all about functions here. What you’ll learn General look on functions Parameters Returning Examples General look on functions Functions have 4 components to them: name, parameters, return type, and code. Name is the name of the function, you will see more about this later. Parameters are inputs that the function can take in when executed, the functions can interact with these inputs to make diverse outcomes. Return types are required if you want to return something. It basically says what something should expect a function to return. What the function returns must follow this type. A few examples of types are string, numbers, player, etc. Code is the snippet that the function executes when it is called. This is what a function looks like: function name(parameter_name: parameter_type) :: return_type: ____# code___________________________________________________ Note that after putting the parameter’s name and type in the function’s declaration, you can also tack on a default value. The parameter’s content becomes the default value if that parameter was not filled in when the function was called. It looks like this: _______________________________________________ parameter_name: parameter_type = default value_ _______________________________________________ To call a function in code, you put the name, and then round brackets, and put your parameters inside of the brackets, separated by commas. It looks like this: ___ _______________________ name({_par1}, {_par2}, ...) _ ____ ______________________ Parameters Parameters can be passed when calling a function. These parameters can be anything, as long as it follows the type specified in the function’s declaration. A function can access the value of a parameter in a local variable, with the parameter’s name as the label. This is what it looks like: ________________ {_parametername} ________________ Returning The return keyword can be used to return a value from a function, this can be thought of a conversation between the function and the code, with parameters and returns. Code calls function -> Passes in parameters -> Function does magic with parameters and whatnot -> Returns value -> Code does stuff with new value Note here that even when a return type is defined in the function, the function does not have to return something. It can choose to return nothing. Here is the syntax for returning: ______________ _+ return %objects% _ _______________ + Examples ______________________________________________________________________function hello(name: text) :: text:___________________________________ ____return “Hello, %{_name}%!”________________________________________ ______________________________________________________________________ hello(“Goose”) # Returned value: “Hello, Goose!”______________________ ______________________________________________________________________ function set(name: text, value: object):______________________________ ____set {vars::%{_name}%} to {_value}_________________________________ ______________________________________________________________________ set(“foo”, “bar”)_____________________________________________________ {vars::foo} # Value: “bar”____________________________________________ Conclusion Functions are a very nifty tool that can be used to make code cleaner and easier to manage! I hope this tutorial covered most of functions, and feel free to comment anything I missed. 3 VIP since January 7th, 2020 Support since September 19th, 2020 Helper since November 3rd, 2020 Moderator since March 8th, 2021 Link to comment Share on other sites More sharing options...
SkripterMan Posted January 2, 2021 Share Posted January 2, 2021 Looks great! SkripterManJoined Minehut on 4/21/16 Owner of CareSMP.minehut.ggVIP - 6/21/17LEGEND - 18/12/20MARKET MAKER - 5/6/21Discord: SkripterMan#0001 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now