### Author Topic: [Solved]Assign argument variables in function parameter?  (Read 627 times)

#### zutokaza

• Full Member
• Posts: 146
• Active - Making stories can take the most time.
##### [Solved]Assign argument variables in function parameter?
« on: May 16, 2017, 07:20:13 am »
How would I do something like this?

Code: Lua
1. function test(a = 5, b = "hey", c = true)
2.   if a == 5 then
3.     print("Letter A is",a)
4.   end
5. end
6.
7. test()

I know it can be done like this:

Code: Lua
1. function test(a, b, c)
2. a = 5
3. b = "hey"
4. c = true
5.   if a == 5 then
6.     print("Letter A is",a)
7.   end
8. end
9.
10. test()
« Last Edit: May 25, 2017, 03:21:06 am by zutokaza »

#### MetalZelda

• Hero Member
• Posts: 544
##### Re: Assign argument variables in function parameter?
« Reply #1 on: May 16, 2017, 11:30:28 am »
How would I do something like this?

Code: Lua
1. function test(a = 5, b = "hey", c = true)
2.   if a == 5 then
3.     print("Letter A is",a)
4.   end
5. end
6.
7. test()

I know it can be done like this:

Code: Lua
1. function test(a, b, c)
2. a = 5
3. b = "hey"
4. c = true
5.   if a == 5 then
6.     print("Letter A is",a)
7.   end
8. end
9.
10. test()

Code: Lua
1. function test(a, b, c)
2.   if a == 5 then
3.     print("letter A is " .. a)
4.     -- You can even modify your variables from there
5.   end
6.   return a, b, c
7. end
8.
9. test(5, "hey", true)
10.

The arguments should be when you call that function, not when you define it.
Of course, "return a, b, c" is optionnal, it's only when you want to store a, b or / and c in a variable, but since the function is called "test", you might wanna return some result somewhere else
« Last Edit: May 16, 2017, 11:33:26 am by MetalZelda »

#### zutokaza

• Full Member
• Posts: 146
• Active - Making stories can take the most time.
##### Re: Assign argument variables in function parameter?
« Reply #2 on: May 24, 2017, 08:05:43 pm »
Yes, I know of that method too. I want to know if it is possible to do like the surface create function. I would like to assign variables in a table for easier reading.

Code: Lua
1. sol.text_surface.create({ -- name a local variable something and assign it to the sol.text_surface
2.       font = "minecraftia", -- font name
3.       text = "what", -- text you want to show
4.       font_size = 50, -- font size obviously
5.       horizontal_alignment = "center", -- default "left"
6.       vertical_alignment = "bottom", -- default "middle"
7.       rendering_mode = "antialiasing", -- "solid" (faster) and default
8.       color = {0,0,0}, -- color must be in a table RGB (http://www.rapidtables.com/web/color/RGB_Color.htm)
9.     })
« Last Edit: May 24, 2017, 08:07:14 pm by zutokaza »

#### llamazing

• Full Member
• Posts: 106
##### Re: Assign argument variables in function parameter?
« Reply #3 on: May 25, 2017, 01:03:08 am »
In that case you simply make a function that takes a table for the first parameter:
Code: Lua
1. function test(info)
2.     if info.a == 5 then
3.         print("Letter A is",info.a)
4.     end
5. end
6.
7. test{a=5, b="hey", c=true}

Keep in mind that the table passed to the function may not have values defined for every entry in the table. It also might not even be a table that gets passed to the function, so be sure your code handles all the possibilities.

#### zutokaza

• Full Member
• Posts: 146
• Active - Making stories can take the most time.
##### Re: Assign argument variables in function parameter?
« Reply #4 on: May 25, 2017, 03:20:23 am »
@llamazing
Exactly what I wanted. Thanks again for your assistance!