static void Create()
{
for(int i = 0; i < 3; i++)
{
Console.WriteLine("★게임 아이템 생성★");
}
}
static void Main(string[] args)
{
Create();
}
결과값:
★게임 아이템 생성★
★게임 아이템 생성★
★게임 아이템 생성★
여기서 매개변수는 health
인수는 currentHealth
static void Health(int health)
{
Console.WriteLine(health);
}
static void Main(string[] args)
{
int currentHealth = 100;
Health(currentHealth);
}
결과값: 100
쉽게 말하자면, 메인함수에 있는 x와 y값이 참조 형태 인자로 매개변수로 전달되기 때문에, 안에서 값이 바뀌면 밖에서도 바뀐 값이 적용된다

static void Swap(ref int x, ref int y)
{
int temporary = y;
y = x;
x = temporary;
}
int x = 10;
int y = 20;
// 이렇게 하면 값이 Swap되지 않는다
Swap(x, y);
Console.WriteLine("x의 값은: " + x);
Console.WriteLine("y의 값은: " + y);

static void Swap(ref int x, ref int y)
{
int temporary = y;
y = x;
x = temporary;
}
static void Main(string[] args)
{
int x = 10;
int y = 20;
Swap(ref x, ref y);
Console.WriteLine("x의 값은: " + x);
Console.WriteLine("y의 값은: " + y);
}
결과값: x의 값은: 20 / y의 값은: 10
static void Score(char grade, out int score)
{
switch(grade)
{
case 'A': score = 100;
break;
case 'B':
score = 90;
break;
case 'C':
score = 80;
break;
case 'D':
score = 70;
break;
default: score = 0;
break;
}
}
static void Main(string[] args)
{
int score = 0;
Score('A', out score);
Console.WriteLine("score 변수의 값 : " + score);
}
결과값: score 변수의 값 : 100
case A에 해당하는 값인 100
static void Receive(in int packet)
{
// packet = 99; 값 변경 불가능
Console.WriteLine("packet : " + packet);
}
static void Main(string[] args)
{
int data = 100;
Receive(data);
}
결과값: 100
매개 변수 현재 x = 0, y = 0
default 매개변수 = 0 (기본적으로 아무것도 인자로 안넘기면 들어있는 값)
static void Position(int x, int y = 1)
{
Console.WriteLine("x의 좌표 : " + x);
Console.WriteLine("y의 좌표 : " + y);
}
static void Main(string[] args)
{
Position(2);
}
결과값:
꼭 재귀를 탈출 할 수 있는 조건의 return문을 가장 위에 써두자!!! 나중에 알고리즘에서 정말 많이 쓴다 (지금의 나는 모르겠지만 알아두자 제발)
static void Connect(int count)
{
if(count <= 0)
{
return;
}
Console.WriteLine("Connection....");
Connect(count - 1);
}
static void Main(string[] args)
{
Connect(3);
}
결과값: Connection.... Connection.... Connection....