C# 기초강의 8_ 함수

Park Yeeun·2023년 6월 29일
0

C# 기초공부

목록 보기
8/10

📌 함수란

특정 목적, 기능을 하도록 정의된 코드가 모여서 만들어짐
ex) System 라이브러리의 WriteLine()

✔ 사용이유

코드의 재사용 (중복사용 가능성이 높을경우 사용)

✔ 형태

int addsumnumber(int a, int b)	// 매개변수
{
	int c = a + b;	// 실행코드
	return c;		// 리턴값
}

📌 연습문제 1

💬 코드

static void Main(string[] args)
{
	int a = 5;
	int b = 7;
	int c = addsum(a, b);

	Console.WriteLine(c);	// 12
}

static int addsum(int a, int b)
{
	return a + b;
}

📌 연습문제 2

💬 코드

static void Main(string[] args)
{
  int a = 5;
  int b = 7;

  Class1 c1 = new Class1();
  int c = c1.addsum(a, b);

  Console.WriteLine(c);		// 12

  c1.sound();				// 소리함수
}
internal class Class1
{
	public int addsum(int a, int b)
	{
		int c = a + b;
		return c;
	}

	public void sound()
	{
		Console.WriteLine("소리함수");
	}
}
profile
새싹 개발자입니당 🌱

0개의 댓글