hihelllo 2018. 2. 27. 09:23

07_함수(1)

함수 정의

1)

var 함수이름 = function() {
//코드
}

2)

function 함수이름() {
//코드
}

매개인자 함수 정의

함수이름() 괄호 안에 매개 인자 개수만큼 매개 변수를 씁니다.

function 함수이름(매개변수1, 매개변수2) {
//코드
}

반환 값이 있는 함수 정의

function 함수이름(매개변수1, 매개변수2) {
//코드
return 반환값;
}

함수 호출

1) 무인자 함수 호출

함수이름();


2) 매개인자가 있는 함수 호출

함수이름(매개인자1, 매개인자2);


3) 매개인자와 반환 값이 있는 함수 호출

var result = 함수이름(매개인자1, 매개인자2);

실습예제

예제 07-1.html

매개인자로 배열을 받아 배열의 원소를 모두 출력해주는 함수 예제입니다.

if (!window.T) { window.T = {} } window.T.config = {"TOP_SSL_URL":"https://www.tistory.com","PREVIEW":false,"ROLE":"guest","PREV_PAGE":"","NEXT_PAGE":"","BLOG":{"id":2859442,"name":"hihellloitland","title":"27","isDormancy":false,"nickName":"hihelllo","status":"open","profileStatus":"normal"},"NEED_COMMENT_LOGIN":false,"COMMENT_LOGIN_CONFIRM_MESSAGE":"","LOGIN_URL":"https://www.tistory.com/auth/login/?redirectUrl=https://hihellloitland.tistory.com/m/38","DEFAULT_URL":"https://hihellloitland.tistory.com","USER":{"name":null,"homepage":null,"id":0,"profileImage":null},"SUBSCRIPTION":{"status":"none","isConnected":false,"isPending":false,"isWait":false,"isProcessing":false,"isNone":true},"IS_LOGIN":false,"HAS_BLOG":false,"IS_SUPPORT":false,"IS_SCRAPABLE":false,"TOP_URL":"http://www.tistory.com","JOIN_URL":"https://www.tistory.com/member/join","PHASE":"prod","ROLE_GROUP":"visitor"}; window.T.entryInfo = {"entryId":38,"isAuthor":false,"categoryId":766226,"categoryLabel":"프로그래밍/Javascript"}; window.appInfo = {"domain":"tistory.com","topUrl":"https://www.tistory.com","loginUrl":"https://www.tistory.com/auth/login","logoutUrl":"https://www.tistory.com/auth/logout"}; window.initData = {}; window.TistoryBlog = { basePath: "/m", url: "https://hihellloitland.tistory.com", tistoryUrl: "https://hihellloitland.tistory.com", manageUrl: "https://hihellloitland.tistory.com/manage", token: "Ri9GKJHJjACvo8hWXbTurEk1v8DBn1uMqTyuR8ErDBs9DzqPp2uAUHfhfYpq07wB" }; var servicePath = ""; var blogURL = "";window.T.gnbContext = {"userId":0,"userName":"","profileSrc":"https://t1.daumcdn.net/tistory_admin/blog/admin/profile_default_03.png","storyServiceURI":{"storyHome":"https://storyhome.kakao.com/","brunchStory":"https://brunch.co.kr/","kakaoStory":"https://story.kakao.com/"},"blogs":[],"tryAutoLogin":false}; \n \n \n\n"}}" data-ve-attributes="{"typeof":"mw:Extension/syntaxhighlight","about":"#mwt3"}">
<html>
<head>
    <script type="text/javascript">
        function arrayPrint(array){
            for(index in array){
                document.write(array[index]+"<br>");
            }
        }

        var jumsu=[100,95,83,94,99,99];
        arrayPrint(jumsu);
    </script>
</head>
</html>

예제 07-2.html

매개인자로 배열을 받아 배열을 정렬해주는 함수입니다.

if (!window.T) { window.T = {} } window.T.config = {"TOP_SSL_URL":"https://www.tistory.com","PREVIEW":false,"ROLE":"guest","PREV_PAGE":"","NEXT_PAGE":"","BLOG":{"id":2859442,"name":"hihellloitland","title":"27","isDormancy":false,"nickName":"hihelllo","status":"open","profileStatus":"normal"},"NEED_COMMENT_LOGIN":false,"COMMENT_LOGIN_CONFIRM_MESSAGE":"","LOGIN_URL":"https://www.tistory.com/auth/login/?redirectUrl=https://hihellloitland.tistory.com/m/38","DEFAULT_URL":"https://hihellloitland.tistory.com","USER":{"name":null,"homepage":null,"id":0,"profileImage":null},"SUBSCRIPTION":{"status":"none","isConnected":false,"isPending":false,"isWait":false,"isProcessing":false,"isNone":true},"IS_LOGIN":false,"HAS_BLOG":false,"IS_SUPPORT":false,"IS_SCRAPABLE":false,"TOP_URL":"http://www.tistory.com","JOIN_URL":"https://www.tistory.com/member/join","PHASE":"prod","ROLE_GROUP":"visitor"}; window.T.entryInfo = {"entryId":38,"isAuthor":false,"categoryId":766226,"categoryLabel":"프로그래밍/Javascript"}; window.appInfo = {"domain":"tistory.com","topUrl":"https://www.tistory.com","loginUrl":"https://www.tistory.com/auth/login","logoutUrl":"https://www.tistory.com/auth/logout"}; window.initData = {}; window.TistoryBlog = { basePath: "/m", url: "https://hihellloitland.tistory.com", tistoryUrl: "https://hihellloitland.tistory.com", manageUrl: "https://hihellloitland.tistory.com/manage", token: "Ri9GKJHJjACvo8hWXbTurEk1v8DBn1uMqTyuR8ErDBs9DzqPp2uAUHfhfYpq07wB" }; var servicePath = ""; var blogURL = "";window.T.gnbContext = {"userId":0,"userName":"","profileSrc":"https://t1.daumcdn.net/tistory_admin/blog/admin/profile_default_03.png","storyServiceURI":{"storyHome":"https://storyhome.kakao.com/","brunchStory":"https://brunch.co.kr/","kakaoStory":"https://story.kakao.com/"},"blogs":[],"tryAutoLogin":false}; \n \n \n\n"}}" data-ve-attributes="{"typeof":"mw:Extension/syntaxhighlight","about":"#mwt3"}">
<html>
<head>
    <script type='text/javascript'>
        function sortArray(array){
            for(var i=0; i<array.length; i++){
                for(var j=i+1; j<array.length;j++){
                    if(array[i]>array[j]){ // 뒤의 배열 값이 작으면 앞의 값과 교체
                        var temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    }
                }
            }
        }
        function arrayPrint(array){
            for(index in array){
                document.write(array[index]+"<br>");
            }
        }
        var jumsu=[100,95,83,94,99,99];
        sortArray(jumsu); // 배열이 정렬됨
        arrayPrint(jumsu); // 정렬된 배열을 출력
    </script>
</head>
</html>