I'm learning how to properly script and I'm kinda confused with the "()" after the functions, can someone help?
Oh boy, not even sure I'm going to use the correct wording for this. Let me start with an example:
function HelloWorld() print('Hello World') end
The ()
after the function name is an essential part for functions to operate. Inside them hold arguments/parameters in which you can pass different objects.
In my own words, I'd say the ()
is used to declare the function as well, being a function. The same way function
is needed to define it, ()
is also needed. Get it?
Functions are machines that take int values, do some work, and spit some values out.
The values that you take in go inside ()
after the function's name and are called arguments. You give names to those parameters in the definition of the function.
Here's a simple example of a function using two parameters:
-- call the first argument `a` -- call the second argument `b` function sayThings(a, b) print("the first argument is", a) print("the second argument is", b) end sayThings(1, 2) --> the first argument is 1 --> the second argument is 2
Functions may also take no arguments. Since they can still do something when you call them, you distinguish them from regular old names by still including a ()
at the end.
Putting ()
after a function name calls or invokes that function, which runs the code in its definition:
-- this function doesn't take any arguments -- it just prints a greeting when it is called function sayHi() print("hello") end -- this invokes sayHi: x = sayHi() --> hello -- this does NOT invoke sayHi y = sayHi --> [no output]
Well, when a function is called, the user can put variables in the function. So these variables are called parameters. The values that these variables are set to are called arguments. Remember that the parameter is local to the function and used in only the function's scope and it's descending scopes. Let me show you with text what i mean
function bob(x, y) -- x and y are the parameters of bob print(x, y) -- as you see they are used later in the function end -- now if i call the function... bob('Hi', 'Bye') -- These two values are called arguments, these will be set as the parameter. -- so now when it prints, it will substute x and y with the strings Hi and Bye
hope this helps and sorry if my english is bad
Okay I put a comment but I don't think I explained it well. You don't always have to use () This for an example wouldn't work though,
function Hey() print("Hey") end Hey
You have to put the () after it to call it. This is not the only way to call a function. For a string, you can call a function like this
function Print2(msg) print(msg) end Print2'Hello World' --or Print2[[Hello World]]
or even for a table.
function PrintType(inp) print(type(inp)) end PrintType{"TableIndex1"} => "table"