# return값이 있는 함수

## return값이 있는 함수

> function 함수이름(){\
> &#x20;   //실행코드\
> &#x20;   return 리터값;\
> }\
> let 변수 = 함수명();    //함수호출&#x20;

```javascript
function func4(){
    let str ="함수가 실행되었습니다."
    return str;
}
let value = func4();
document.write(value);
```

### return값+매개변수

```javascript
function func5(num1, num2){
    return num1 + num2
}
let result = func5(100, 200);
document.write(result);
```

### 리턴값이 있는 함수 (종료)

```javascript
function func6(){
    document.write("함수가 출려되었습니다6.");
    return;
    document.write("함수가 출력되었습니다7.")
}
func6;
```

### 평균구하기

```javascript
function testAvg(arrData){
    let sum = 0;
    for( let i=0; i<arrData.length; i++){
        sum += Number(prompt(arrData[i]+"점수는?","0"));
    }
    let avg = sum / arrData.length;
    return avg;
}
let arrSubject = ["국어","수학"];
let result = testAvg(arrSubject);
```

### 이미지 슬라이드

```markup
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="galleryZone">
        <p><img id="photo" src=img/pic_1.jpg" alt="이미지1" style="width: 500px;"></p>
         <div>
             <button onclick="gallery(0)">이전</button>
             <button onclick="gallery(1)">다음</button>
         </div>
    </div>

    <script>
        let num = 1;
        function gallery(direct){
            if(direct){
                if(num==8){
                    return;
                }
                num++;
            } else {
                if(num == 1){
                    return;
                }
                num--;
            }
            console.log(num);
            let imgTag = document.getElementById("photo");
            imgTag.setAttribute("src", "img/pic_"+num+".jpg");    //속성값 설정
        }
    </script>
</body>
</html>
```

```markup
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>javascript40</title>
</head>
<body>
    <div id="galleryZon">
        <p><img id="photo" src="img/pic_1.jpg" alt="이미지1" style="width: 500px;"></p>
        <div>
            <button onclick="gallery(0)">이전</button>
            <button onclick="gallery(1)">다음</button>
        </div>
    </div>
    <script>
        let num = 1; 
        function gallery(direct){
            if(direct){
                if(num == 8){
                    return;
                }
                num++;
            }else{
                if(num == 1){
                    return;
                }
                num--;
            }
            console.log(num);
            let imgTag = document.getElementById("photo");
            imgTag.setAttribute("src", "img/pic_"+ num +".jpg")
        }


    </script>
</body>
</html>
```
