-
파이썬 함수 호출하기Computer Engineering/Python 2023. 3. 7. 11:58
파이썬 함수 호출하기
수학적 정의
- f(x) = x + 2 => f(2) => 4
정의 / 호출 구조
함수는 정의하고 호출하는 방식
정의 하는 방법
- def
def 함수명 (인자1, 인자2, ...): #argument or parameter 구문1 구문2 구문3 ... return 아웃풋 # output이 있는 경우 !
def f(wow_x) : result = wow_w + 2 return result
def wow_f(wow_x, wow_y): return wow_x - wow_y
- 함수 정의 할 때 바깥 문단에서는 접근할 수 없음
호출 하는 방법
def f(wow_x): result = wow_x + 2 return result f(2) = 4
wow_f_result = wow_f(2, 3)
wow_f(wow_x=2, wow_y=3) wow_f(wow_y=3, wow_x=2) #순서 무관
wow_f(3, wow_y=2) # 항상 Postional Argument가 먼저 나오고, Keyword Argument가 뒤에 나와야함!!
함수의 4가지 유형
- input O / output O
def calculate_sum(x, y): return x + y calculate_sum(1, 2) 3
- input O / output X
def save_data_in_database(x): print(x, "를 데이터베이스에 저장하겠습니다") print("저장 완료되었습니다") save_data_in_database(3) 3 를 데이터베이스에 저장하겠습니다 저장 완료되었습니다 ## OUTPUT이 없다는 건 Return이 없다 ## 출력은 있으나 함수적 Output이 아니다
- input X / output O
def get_pi(): return 3.141592 get_pi() 3.141592
def get_current_time(): import time return time.time() current_time = get_current_time() current_time 1678760733.4841647
- input X / output X
def show_current_time(): import time print(time.time(), "입니다") show_current_time() 1678760966.3701527 입니다 ## 화면상의 출력값은 있으나 ## 함수상의 input이 없고 return도 없다
함수의 cascading
Cascading : '위에서 아래로 흐르는', 상속 또는 종속하는'
더보기참고문헌
박응용 저, 「점프 투 파이썬」, 2023
https://opentutorials.org/course/2418/13409
https://makinghome.tistory.com/67
https://velog.io/@bami/CSS-Cascading
⊙ 이 글은 개인 공부를 목적으로 작성된 글입니다.
⊙ 내용에 대한 오류나 피드백 감사히 받고 있습니다 !
반응형'Computer Engineering > Python' 카테고리의 다른 글
파이썬 변수 / 자료형 (0) 2023.03.07 Python 개발환경 세팅 (0) 2023.03.07