- 함수란?
- 인자, 매개변수(parameters)
데이터타입 변수명(인자값,매개변수)
{
기능구현
}
- 반환값이 있는 경우와 없는 경우
DataType FuncName()
{
return Data;
}
void FuncName()
{
// 반환값이 없기 때문에 특정 기능 자체를 담당하는 함수가 됨
}
★ 매개변수 한정자 <-- 중요
Call By Reference : 함수 호출 시 원본의 메모리 주소를 전달
Call By Value : 함수 호출 시 복사본 생성
static void SwapNumbers2(ref int first, ref int second)
{
// first, second는
// 입력받은 인자의 데이터가 존재하는 메모리 주소를 전달 받음
// call by reference
int temp = first;
first = second;
second = temp;
}
static void SwapNumber1(int first, int second)
{
// first, second는
// 입력받은 인자의 "복사본"이 된다.
// call by value
int temp = first;
first = second;
second = temp;
}
static void RefTest(ref int x)
{
Console.WriteLine(x); // 읽기
x = 5; // 쓰기
}
static void InTest(in int x)
{
Console.WriteLine(x); // 읽기
x = 5; // 쓰기(에러)
}
static void OutTest(out int x)
{
// 에러(인자에 값을 할당하지 않음)
}
static void OutTest(out int x)
{
Console.WriteLine(x); // 초기화 되기 전 읽기(에러)
x = 5; // 쓰기
}