> For the complete documentation index, see [llms.txt](https://pretender.gitbook.io/beginners-scripting-guide-beta/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pretender.gitbook.io/beginners-scripting-guide-beta/function.md).

# Function

<https://create.roblox.com/docs/luau/functions#basic-functions>\
<https://create.roblox.com/docs/scripting/events/remote#argument-limitations>\
\
In this section, we will be discussing about function. A function is a code of block that can hold another code of block and can execute the code block that was stored in the function multiple times by using parentheses to call out the function.

### MAKING A FUNCTION

Making a function is the same as making a variable. But it is just a little bit different. Instead of:

```lua
local myFunction
```

We can do it like this:

```lua
local function myFunction()

end
```

### **CALLING A FUNCTION**

Now we have a function. But the function will not run yet unless you call it. To call it we will get it name and put parentheses after the name like this:

```lua
local function myFunction()
    print('Hello, World!!')
end

myFunction() -- Call the function to let it print "Hello, World!!" in the output
```

### **GLOBAL & LOCAL FUNCTION**

A function also be <mark style="color:blue;">**GLOBAL**</mark> and <mark style="color:red;">**LOCAL**</mark>. <mark style="color:blue;">**GLOBAL**</mark> means it can be accessed everywhere in the script and <mark style="color:red;">**LOCAL**</mark> means it can be only accessed in a certain block of codes. It is the same as <mark style="color:blue;">**GLOBAL**</mark> and <mark style="color:red;">**LOCAL**</mark> variable.

### GLOBAL

```lua
if 3 > 1 then
     function sayHello()
          print('Hello, World!!')
     end
     sayHello() -- Hello, World!!
end

sayHello() -- Hello, World!!
```

### **LOCAL**

```lua
if 3 > 1 then
     local function sayHello()
          print('Hello, World!!')
     end
     sayHello() -- Hello, World!!
end

sayHello() -- Error "attempt to index call nil" because the function is not global so say hello function is nil!!
```

### ADDING PARAMETERS TO FUNCTION

Now let's say we want the function to print the total sum of 2 numbers that was given in the function when it was called. We can start doing that by making 2 parameters with different names like this:

```lua
local function myFunction(num1, num2)

end
```

Now we are adding 2 parameters together and printing it in the function:

```lua
local function myFunction(num1, num2)
    local result = num1 + num2
    print('The sum of '..num1..' and '..num2..' is: '..result)
end
```

Now we gonna call the function and pass in 2 arguments for the function:

```lua
local function myFunction(num1, num2)
    local result = num1 + num2
    print('The sum of '..num1..' and '..num2..' is: '..result)
end

myFunction(1, 1) --- The sum of 1 and 1 is: 2
```

### USING RETURN IN FUNCTION

Return can be used to return data to the function or just break the function to prevent the code below from running.

### RETURNING DATA BACK TO FUNCTION

To return data, we must make a function first and then use <mark style="color:red;">`return`</mark> and put the data that we want to return to the function after the <mark style="color:red;">`return`</mark>

### RETURN A SUM TO FUNCTION

```lua
local function getSumOf1Plus1()
     return 1 + 1 -- returning the sum of 1 + 1 back to function.
end

local result = getSumOf1Plus1()
print('1 + 1 = '..result) -- 1 + 1 = 2
```

### BREAKING A FUNCTION

```lua
local a = true

local function runAFunc()
	--- If a is false then return.
	if not a then
		return
	end
	--- Else continue the code below.
	
	print('Hello, World!')
end

a = false

local result = runAFunc()
print(result) --- nil since we return nothing when "a" is false in script.
```
