# 함수

## 함수

### 함수의 형식

```javascript
function 함수명( [인자...[,인자]] ){
   코드
   return 반환값
}
```

함수는 하나의 로직을 재실행 할 수 있으므로 **재사용성이 높다. (유지보수에 용이)**

### 함수의 정의와 호출

```javascript
function numbering(){
    i = 0;
   while(i < 1
        document.write(i);
        i += 1;
    }   
}
numbering(); // 0123456789
```

**numbering();** :point\_right: **함수를 호출할때 형식**&#x20;

### 반복문과 함수의 차이

* **반복문**은 **일정 반복**을 **그자리에서 실행**함&#x20;
* **함수**는 **반복적으로 필요한 로직** **어디서든** 자유롭게 **호출이 가능**하다.&#x20;

### 입력과 출력

**함수의 기본적인역할**  :point\_right: **입력된 값을 연산해서 출력하는 것**

```javascript
function get_member1(){
    return 'egoing';
}
 
function get_member2(){
    return 'k8805';
}
 
alert(get_member1()); //egoing
alert(get_member2()); //k8805



function get_member(){
    return 'egoing';
    return 'k8805';
    return 'sorialgi';
}
alert(get_member()); // egoing
```

* 1번째 , 2번째 함수는 각각의 egoing, k8805를 출력한다. (각각의 함수 내에서의 값을 return하기 때문)
* 3번째 함수 에서는 return 'egoing'을 실행한 후에 함수가 종료된다.&#x20;

### 인자

**함수로 유입되는 입력 값을 의미**

```javascript
function get_argument(arg){
    return arg *1000;
}
 
alert(get_argument(1)); // arg = 1 -> 1000
alert(get_argument(2)); // arg = 2 -> 2000
```

* arg :point\_right:매개변수( parameter ) -> return 하는것을 출력&#x20;
* alert(get\_argumen&#x74;**(1)** ) :point\_right:인자( argument)  -> 입력&#x20;

![](https://810537134-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MZh1fxUzW3zjggiSfv4%2F-MZum7S7w_47xQERuFUQ%2F-MZum_PVVVc-MvzKvlxg%2F%EC%BA%A1%EC%B2%98.PNG?alt=media\&token=983dfab5-88ea-4143-9424-929ec6c15990)

### 복수 인자&#x20;

**여러개의 입력 값을 받음**&#x20;

```javascript
function get_arguments(arg1, arg2){
    return arg1 + arg2
}
 
alert(get_arguments(10, 20)); //30
alert(get_arguments(20, 30)); //50
```

### 함수를 정의 하는 다른 방법

```javascript
var numbering = function (){
    i = 0;
    while(i < 10){
        document.write(i);
        i += 1;
    }   
}
numbering(); //좌항의 numbering 이라는 변수 우항의 함수를 갖게됨 


// 익명함수 (정의와호출을 같이함)
(function (){
    i = 0;
    while(i < 10){
        document.write(i);
        i += 1;
    }   
})(); // 함수가 정의되고 정의된 함수를 묶음 -> (); 정의된 함수를 바로 호출함 
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://leeboa.gitbook.io/study/undefined-13/undefined-5.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
