#whats the difference between a function and a parameter

37 messages · Page 1 of 1 (latest)

hushed ravine
#

A function is a piece of code that only runs when called

#

A parameter (also known as a param) are variables only accessible inside that specific function.

#

e.g.

local function FunctionName(Parameter1, Parameter2)
return Parameter1+ Parameter2
end

#

Well officially yes but let’s be honest, you will never hit that limit

#

You don’t have to

#

let me type out an example (I’m on phone so it may take a second)

#

local function sum(num1, num2)
return num1+num2
end

local Number1 = 10
local Number2 = 13

print(sum(Number1, Number2))

#

Sorry wait

#

Set what in the function?

#

Yes

#

Functions are mainly used for pieces of code that need to run multiple times

#

This can either be done by connecting it to events so they only run when something happens, or you can change out the variables

#

let’s say you have a function for a math equation that you don’t wish to always retype

then you can add that equation into a function and call it for every number you need it for

#

Sometimes this is also done with abbreviations (like 1300-> 1.3k, etc.) and other things

#

Do you know what a scope is?

#

In scripting terms, not the scope of a gun

#

So a scope is basically a “layer” where the script compiled in. E.g. a function

#

If you return inside a function then it will exit the function and return to the line where you called it

#

if you return outside of a function then the script will end (e.g. some scripts you only want to have compile when you’re testing in studio but not outside of studio)

#

It will also return the given value if any

hushed ravine
#

local function sum(num1, num2)
print(“Check1”)
return num1+num2
print(“Check2”)
end

local Number1 = 10
local Number2 = 13

local Solution = sum(Number1+Number2)
print(Solution)

#

If you compile (run) this script then you’ll notice a few things

#

To save you the trouble I’ll tell you what it will show in the console

#

Check1
23

#

It doesn’t print Check2, as that’s a line under the return, this means the compiler won’t ever reach it

#

the line
local Solution = sum(…)
will let the function run, and wait for it to return something

#

Then once the function finishes it will receive the number “23”

ripe swiftBOT
#

studio** You are now Level 2! **studio

hushed ravine
#

as we made it return num1+num2 (so 10+13)

#

Do you understand the part about returning a value?

#

Yes not the exiting scope part, okay

#

So the compiler always compiles top to bottom, once it hits “return” it will return the value given (or nil if there isn’t anything to return). And then stop looking at that function

#

No

#

Return to the line that called the function

#

To be frank, just mess around with it for an hour and you’ll understand

hushed ravine