TOC
전달값과 반환값
예시 1
- 잔액(balance)에 입금액(money)을 전달받아 입금하는 함수
def deposit(balance, money):
print("입금이 완료되었습니다. 잔액은 {0}원입니다.".format(balance + money))
return balance + money
balance = 0
balance = deposit(balance, 1000)
"""
deposit(balance, money) 함수 실행하여
balance = 0 에 money(1000) 입금하고
잔액 알리는 print문 실행 후
balance에 입금액 추가
"""
print(balance)
예시 2
def withdraw(balance, money):
if balance >= money:
print("출금이 완료되었습니다. 잔액은 {0}원입니다.".format(balance - money))
return balance - money
else:
print("출금액이 잔액보다 많습니다. 잔액은 {0}원입니다.".format(balance))
return balance
balance = 0
balance = deposit(balance, 1000)
balance = withdraw(balance, 2000)
balance = withdraw(balance, 500)
예시 3
- 밤에 출금하는 경우
- 밤에 출금하는 경우 수수료가 붙는다고 가정
- 수수료는 100원
def withdraw_night(balance, money):
commission = 100
return commission, balance - money - commission
balance = 0
balance = deposit(balance, 1000)
commision, balance = withdraw_night(balance, 500)
print("수수료는 {0}원이며, 잔액은 {1}원입니다.".format(commision, balance))
Reference