.NET프로그래밍/C# 3.5 SP1

18. 매개변수가 있는 함수

holland14 2009. 8. 5. 17:30

// 매개변수 == 파라미터 == 인자 == 인수 ==
// Paremeter == Argument == 가인수 == 실인수
// 넘겨주고받는 값을 저장하는 변수

using System;

public class 매개변수가있는함수
{
    public static void Main()
    {
        Hello("안녕");// "안녕"
        Hello("방가");// "방가"
        Hello("또봐");// "또봐"
        Say("낼봐", 3);

       
        // Quiz : 아래기능을 하는 함수를 설계해보시오...
        Calc(3, '+', 5); // 8
        Calc(3, '-', 5); // -2
    }

    public static void Calc(int a, char op, int b)
    {
       
//[1] Input
        int result = 0;
       
//[2] Process
        switch (op)
        {
            case '+': result = a + b; break;
            case '-': result = a - b; break;
            default: break;
        }
       
//[3] Output
        Console.WriteLine(result);
    }


    public static void Hello(string msg)
    {
        Console.WriteLine(msg); // 넘겨온 파라미터값을 출력
    }

    public static void Say(string msg, int cnt)
    {
        for (int i = 0; i < cnt; i++)
        {
            Console.WriteLine(msg); // 2번째 [인자]값만큼 출력
        }
           
    }
}