Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Function format question, what's the "()"?

Asked by 7 years ago

I'm learning how to properly script and I'm kinda confused with the "()" after the functions, can someone help?

4 answers

Log in to vote
2
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
7 years ago

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?

0
But when do I use it? For example in this print one you left it blank. ArcanZus 61 — 7y
0
You use it to call any function. If you put HelloWorld it wouldn't fire, but if you put HelloWorld() it'd fire. You don't always have to include the () though, for example you could do print'Hello world' or ListTable{a = "tableindex1",b = "tableinde2"} fireboltofdeathalt 118 — 7y
0
For correct terminology, the parenthesi proceeding a function keyword indicates the calling of said function. Goulstem 8144 — 7y
Ad
Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago

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]
Log in to vote
1
Answered by
Stadow 30
7 years ago

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

Log in to vote
0
Answered by 7 years ago

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"
0
Ignore SH syntax. fireboltofdeathalt 118 — 7y

Answer this question