본문 바로가기
Article/CODING AND SCRIPTS

Argument와 Parameter

by Roblox_개발자 2021. 4. 6.

parameter는 함수 정의 안에 사용되어지는 변수들(variables)을 뜻한다. argument는 함수를 실행할 때 사용되어지는 값들(values)을 뜻한다. 

local function printStuff(x) -- printStuff함수를 선언하며, parameter는 x이다.
	print(x)
end
 
printStuff("Hello world.") -- printStuff함수를 실행하며, argument는 "Hello world."이다. 

 

parameter를 가진 함수 정의하기 

local function addNumbers(x, y)
	return (x + y) --you might want to learn about the return statement
end

위의 함수는 2개의 parameter를 가진다. 따라서 이 함수를 실행할 때 2개의 argument가 필요하다. 

 

함수 실행하기 

local function addNumbers(x, y)
	return (x + y)
end
 
local sum = addNumbers(2, 5)
print(sum) -- 7

##주의##

함수가 가진 parameter 개수보다 많은 수의 argument를 사용하면, 추가적인 argument들은 무시된다. 반대로 적은 수의 argument를 사용하면, 표시되지 않은 parameter는 nii(null)를 반환한다.

 

Variadic arguments

print()와 같은 글로벌 함수는 숫자 제한없는 argument들을 취할 수 있다. 

print("LOL", "OMG", "C++ IS PRETTY NEAT IF YOU ASK ME", 1241425717231) --4 arguments
print(1, 122, "Hi!", "Roblox", "Give it your best shot!", "C++", "Interesting") --7 arguments

커스텀 함수에서도 print()함수와 마찬가지로 제한없는 argument를 취하는 함수를 만들 수 있다.

local table1 = {}
 
local function appendToTable(tab, ...)
	local args = {...} --you might want to understand tables for this example
	for i, v in pairs(args) do
		table.insert(tab, v)
	end
end
 
appendToTable(table1, "xD", ":D", "lol")

 


참고 링크

developer.roblox.com/en-us/articles/Arguments-and-Parameters

 

Arguments and Parameters

This Platform uses cookies to offer you a better experience, to personalize content, to provide social media features and to analyse the traffic on our site. For further information, including information on how to prevent or manage the use of cookies on t

developer.roblox.com

 

'Article > CODING AND SCRIPTS' 카테고리의 다른 글

Understanding CFrames  (0) 2021.04.10
Roblox Client-Server Model  (0) 2021.04.10
BodyPosition  (0) 2021.04.06
BodyMovers  (0) 2021.04.06
Basic String Patterns (정규식)  (0) 2021.04.06

댓글