Functions and their uses

I’ve watched quite a few tutorials on functions, yet I still don’t quite get what their purpose is. What also confuses me is arguments as they also don’t make sense.

If I have this code

local Function Test()
print(hi)
end

Test()

Why don’t I just write print?

And also what does it mean when you define an argument?

local Funtion Test(2,3)
print(yo)
end

Test(yes,no)

Like, I really don’t understand it. If anyone could help I would appreciate it.

1 Like

I understand now, but when would I ever use this? Also, does a function have a limit on how many arguments it can have?

I don’t think “arguments” have a limit, but I use functions all the time. They’re easy to use and pretty much shorten up your code.

1 Like

So functions are really just a “neatening” thing?

Watch the whole video, functions are pretty fundamental.

There are no practical limits of arguments for functions but general good practice is fewer = better.

2 Likes

I have this “Typewriter” code, I think from AlvinBlox? not sure but yea.

It’s a ModuleScript in ReplicatedStorage.

local TypeWriter = {}

TypeWriter.Type = function(Object, Text)
	for i = 1, #Text, 1 do
		Object.Text = string.sub(Text, 1, i)
		wait(0.05)
	end
end

return TypeWriter

Now, if I’d want to use this I make a LocalScript because I want to Type Write some TEXT.

local typeWriter = require(game.ReplicatedStorage.TypeWriter) --Requiring the modulescript

--Writing the text!
typeWriter.Type(script.Parent.Frame.TextLabel, "Find the key, and open the door!")
-- The TextLabel is the Object, just like the first argument in the ModuleScript. The argument within the speech marks "Find the key, and open the door!"" is the TEXT, the 2nd argument. The modulescript now has every argument needed and it can do what it's supposed to do, Write text!

PRetty bad explanation but you get the point I guess?

I wouldn’t confuse OP with ModuleScripts right now.

What I found worked best for me when starting off was watching the AlvinBlox beginner series in order, that way I always knew what he was talking about because I learnt about it in a previous episode.

This is the series that I watched

Now there is a 2021 version but it’s incomplete there’s only 6 episodes.

2 Likes

Really agree with you. AlvinBlox is probably the best YouTuber/Teacher to watch if you’re a beginner/starter scripter.

2 Likes

Really appreciate it, I can always look at how to use Return and module scripts later. But for my purposes I’ll be handling a lot of repetative lines of code so functions will come in handy.

A function is a group of statements that together perform a task. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually unique, is so each function performs a specific task.

The Lua language provides numerous built-in methods that your program can call. For example, method print() to print the argument passed as input in console.

A function is known with various names like a method or a sub-routine, in some other languages.
Examples -
the “built-in” function print(arg…), writes up things on your output terminal and you see the statement “it carries out a particular task.”
There are many built-in functions such as tick(),tostring(),tonumber().
You can define your own functions this way-

function Add(n1,n2) -- keyword function followed by the identifier, name. 
    local result = n1 + n2
    return result
end

this function takes in 2 arguments, which are independent and the function returns the result ,dependent, which contains the summation of n1 and n2.
You can thus make your own functions and the more you experiment the better you get.

1 Like