본문 바로가기

:SERVER/C#(C Sharp)

함수, ref, out

반응형

1. 함수, ref, out

using System;

namespace CSharp {
    

    class Program{

        // Method, Function, ..함수
        // 한정자 반환형식 이름(매개변수 목록) {} 
        static void HelloWorld() {
            System.Console.WriteLine("Hello World");
        }

        // 복사값을 넘긴다
        static int add(int a, int b){
            return a+b;
        }

        // 참조값을 넘긴다(ref)
        static void addOne(ref int num){
            num+=1;
        }

        // 리턴 값을 여러개 보내고 싶을 때 사용. 참조값을 리턴함(out)
        static void divide(int a, int b, out int result1, out int result2) {
            result1 = a/b;
            result2 = a%b;
        }

        static int addOne2(int num){
            return num+1;
        }

        static void swap(ref int a, ref int b) {
            int temp = a;
            a = b;
            b = temp;
        }

        // Main function 메소드 함수
        static void Main(string[] args) {
            Program.HelloWorld();
            
            int result = Program.add(4,5);
            System.Console.WriteLine(result);

            int num = 3;
            Program.addOne(ref num);
            System.Console.WriteLine(num);

            num = Program.addOne2(num);
            System.Console.WriteLine(num);

            int n1 = 10;
            int n2 = 3;
            int res1, res2;
            divide(n1, n2, out res1, out res2);
        }
    }
}

2. 함수 오버로딩

using System;

namespace CSharp {
    

    class Program{
        // method overoad 메소드 오버로드, 함수 오버로딩
        // 함수 이름의 재사용
        static int add(int a, int b) {
            return a+b;
        }
        static int add(float a, float b) {
            return a+b;
        }
        static int add(int a, int b, int c, int d, int e) {
            return a+b+c+d+e;
        }
        // 반환 형식은 오버로딩에 영향을 주지 않는다
        // 매개변수의 형식과 매개변수의 개수는 영향을 준다


        //선택적 매개변수
        // 4번째, 5번째 인자는 옵션
        static int add(int a, int b, int c, int d=0, double e=3.14) {
            
        }

        // Main function 메소드 함수
        static void Main(string[] args) {
            // 특정 인자를 지정해서 값을 넘길 수 있다.
            add(10, 4, 7, e:10.277);
        }
    }
}

3. 재귀함수

using System;

namespace CSharp {
    

    class Program{

        static int multiply(int a, int b){
            return a*b;
        }

        // 구구단 출력 함수
        static void pirntFunc() {
            for(int i=2; i<10; i++ ) {
                for(int j=1; j<10; j++) {
                    System.Console.WriteLine($"{i} x {j} = {multiply(i,j)}");
                }
            }
        }

        // 별 출력 함수
        static void printStar() {
            for(int i=0; i<5; i++) {
                for(int j=0; j<=i; j++) {
                    System.Console.Write("*");
                }
                System.Console.WriteLine();
            }
        }
        
        // 팩토리얼 함수
        static int factorial(int n) {
            int result = 1;
            for (int i = n; i >= 1; i--) {
                result = result * i;
            }
            return result;
        }

        // 재귀함수, 자기자신을 반복해서 호출
        static int factorial2(int n) {
            if(n<=1) return 1;
            return n * factorial2(n-1);
        }

        // Main function 메소드 함수
        static void Main(string[] args) {
            // pirntFunc();
            // printStar();
            int res = factorial(5);
            System.Console.WriteLine(res);
        }
    }
}
반응형

':SERVER > C#(C Sharp)' 카테고리의 다른 글

클래스, 생성자, static  (0) 2022.09.16
스택,힙 메모리, 클래스  (0) 2022.09.16
반복문  (0) 2022.09.14
분기문(if문, switch문, 삼항연산자)  (0) 2022.09.14
C# 변수, 문자열  (0) 2022.09.14