Functions - Lua Basics (Part 10)

preview_player
Показать описание
Time to stop copy/pasting code! Use functions and reuse the functions as you need them instead!

FAQs:
Is Lua good for beginners/roblox?
Yes, Lua is perfect to learn for both beginners and Roblox developers!

What code editor are you using?
VSCode

What Lua extension are you using?

How do I install Lua (Windows)?

How do I run Lua in VSCode?

Subscribe for more videos!

Group Chats:

You can contact me on:

Any suggestions? Leave it in the comment section or contact me somewhere else!

Odysee Links:

Thank you for watching!

#stevesteacher #lua #learnlua #luabasics
Рекомендации по теме
Комментарии
Автор

I watched so many tutorials on lua and I never properly got functions until this video. Thanks

jag
Автор

idk why people don't appreciate this video

Janfav
Автор

Im beginning to be a little good at this, thanks for your free course, it has been really helpful for my personal projects

DefinedCmajor
Автор

For anyone else that got stuck on this part like I did:

Why does this work:
local function sum(num1, num2)
local ans = num1 + num2
return ans
end

local x = 20
local ans = sum(10, x)
print(ans)

But this doesn't:
local function sum(num1, num2)
print(num1 + num2)
end

local x = 20
local ans = sum(10, x)
print("The answer is: " .. ans)

---

In the first code snippet, you define a function sum that calculates the sum of num1 and num2 and returns the result, which is stored in the ans variable. Then, you call the sum function with 10 and x, where x is defined as 20. The result of this calculation, 30, is stored in the ans variable, and you print ans, so it correctly displays the sum 30.

In the second code snippet, you define the same sum function, but this time, it does not return anything; it only prints the result of the addition to the console. When you call the sum function with 10 and x, it prints 30 to the console, but it doesn't actually return a value. Therefore, the ans variable in the line local ans = sum(10, x) receives nil, because there is no value returned from the sum function. As a result, when you try to concatenate it with a string and print it, you get the error.

If you want to use the result of a calculation performed in a function, outside of that function, you typically need to return the result from the function using the return statement. This allows you to capture the result in a variable when you call the function, and then you can use that variable for various purposes, including printing, further calculations, or any other processing you might need to perform.

If you use print within the function, it will display the value to the console, but it won't provide a way to access or use that value outside of the function unless you return it. Returning values from functions is a fundamental way to pass data from the function to the calling code.

theartofshwa
Автор

How do I prevent desktop video capture programs from running while viewing the application I created to view videos?

يمرحويتعلم
Автор

@stevesteacher, I've added you, I look forward to hearing from you soon as it is urgent😅 speaking of, I'm not sure if I added the right user. Your pfp is the purple haired anime character? Otherwise, great video my man

Lemoni_Frogg