함수 사용법 배우기

생활코딩 - https://opentutorials.org/course/3085/18851

코드

<h1>함수가 뭔가요?</h1>

    <h2>함수 기본 사용법</h2>
    <ul>
      <script>
       /*
       배열을 가진 변수 number를 선언하고 
       sayNumber이라는 함수를 만들어서 이용함
       */
        
        const number = ['하나', '둘', '셋', '넷', '다섯', '여섯'] ;
        function sayNumber(){
        document.write(number);
        }
        sayNumber();
      </script>
    </ul>


    <h2>Parameter? Argument? 뭐지?</h2>
    <script>      
     /*
     a, b 라는 파라메터를 가지는 함수 plus를 만들고 
     상황에 따라 argument를 넣어 사용해보자
     */
      function plus(a, b){
        document.write(a + b+'<br>');
      }      
      function plusColorRed(a, b){
        document.write('<div style="color:red">'+(a + b)+'</div><br>');
      }
      plus(4, 6);
      plus(2, 4);
      plusColorRed(1, 7);
    </script>


    <h2>Return의 사용</h2>
      <script>      
      // 생성한 함수의 리턴값을 이용해서 더 많은 일을 해보자 
      function plus2(a, b){
        return a+b;
      }
      document.write(plus2(2,3)+'<br>');
      document.write('<div style="color:red">'+plus2(2,3)+'</div>');
      document.write('<div style="font-size:3rem;">'+plus2(2,3)+'</div>');
      </script>

Last updated