목차

함수
함수를 호출 할 때 괄호 내부에 여러 가지 자료를 넣는데, 이때의 자료를 매개변수라고 부른다.
함수를 호출해서 최종적으로 나오는 결과값을 리턴값이라고 부른다.
사용 방법
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def function_name(): | |
코드 |
예시
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def print_hello(): | |
print("Hello") | |
print_hello() # Hello |
예시 - 매개변수가 있는 함수
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def multiplication(a, b): | |
print(a * b) | |
multiplication(3, 4) # 12 |
가변 매개변수 함수
함수를 선언할 때의 매개변수와 함수를 호출할 때의 매개변수의 개수가 같아야되지만,
매개변수를 원하는 만큼 받을 수 있는 함수를 가변 매개변수 함수라고 부른다.
사용 방법
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def 함수 이름(매개변수, 매개변수, ..., *가변 매개변수): | |
코드 |
사용 조건
가변 매개변수 뒤에는 일반 매개변수가 올 수 없다.
가변 매개변수는 하나만 사용할 수 있다.
예시
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sum_all(a, b, *numbers): | |
print(a + b + sum(numbers)) | |
sum_all(1, 2, 3, 4, 5) # 15 |
리턴
함수의 결과를 리턴이라고 한다.
리턴을 하게되면 함수를 끝내라는 의미이다.
예시
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def return_example(): | |
print("===A===") | |
return | |
print("===B===") | |
return_example() # ===A=== |
return의 다음줄인 print("===B===")는 출력이 안되는 것을 확인할 수 있다.