여러 매개변수(variable)(parameter) 전달
함수(function)에 여러 개의 값을 전달할 수 있습니다.
int PlusNumber(int a, int b) // 두 개의 매개변수
{
int c = a + b;
return c;
}
사용 예시
void Start()
{
int result = PlusNumber(3, 97); // 3과 97을 전달
Debug.Log(result); // 100 출력
}
- 동작 과정
PlusNumber(3, 97)호출a = 3,b = 97로 설정c = 3 + 97계산 → 100- 100 반환(return)
실전 활용
// 두 점 사이의 거리 계산
float Distance(float x1, float y1, float x2, float y2)
{
float dx = x2 - x1;
float dy = y2 - y1;
return Mathf.Sqrt(dx * dx + dy * dy);
}
// 플레이어 정보 출력
void ShowPlayerInfo(string name, int level, int hp)
{
Debug.Log("이름: " + name);
Debug.Log("레벨: " + level);
Debug.Log("체력: " + hp);
}