학습(공부)하는 블로그 :: 13. 함수에 대하여(2)
 

 

Notice»

Recent Post»

Recent Comment»

Recent Trackback»

04-20 00:00

반응형

1. main() 함수에 대하여


○ 함수를 만들어 사용하는 이유 : 반복적으로 행해지는 각종 연산을 효율적으로 처리하기 위함


○ main() 함수가 받는 인자

- public static void main(String args[])

· String args[] ☜ 문자형 배열

· void ☜ 자료형, 되돌려 주는 값이 없다.


○ 인자 값을 넘겨받는 main() 함수 : main_01.java

 class main_01

 {

         public static void main(String args[])

         {

                 for(int i=0; i < args.length; i++)

                 {

                         System.out.println(args[i]);

                  }

          }

 }


- 실행 결과


2. 오버로딩


○ 오버로딩 : 이름은 같지만 인자가 다른 함수


○ 함수의 오버로딩 예제 : overloading,java

 class overloading

 {

         public static void main(String args[])

         {

                 System.out.println(max(5, 9, 2));

                 System.out.println(max(3.1, 10.33, -32.1));

          }

          /* 정수형 max() 함수 */

         public static int max(int a, int b, int c)

         {

                int max = 0;


                if(a>b)    {

                              max = a;

                }

                else {

                              max = b;

                }

                if(c>max) {

                              max = c;

                }


                return max;


          }


          /* 실수형 max() 함수 */

         public static double max(double a, double b, double c)

         {

                double max = 0;


                if(a>b)    {

                              max = a;

                }

                else {

                              max = b;

                }

                if(c>max) {

                              max = c;

                }


                return max;

          }
 }


- 실행 결과




반응형
: