파이썬 강좌(초급)/6.함수
6.함수 - 2)인자값 & 리턴값
파기차차
2024. 3. 10. 09:41
728x90
반응형
SMALL
1) 인자값(전달값) : 인자값은 함수에 값을 전달하기 위해 사용합니다.
아래 코드의 'deposit(balance, 1000)' 에서 'balance' 와 '1000' 을 인자값으로 보시면 됩니다.
def deposit(balance, money):
print("입금이 완료되었습니다. 잔액은 {0} 원입니다.".format(balance + money))
return balance + money
balance = 0
balance = deposit(balance, 1000)
print(balance)
2) 리턴값(반환값) : 리턴값은 함수가 처리한 내용을 반환할 때 사용합니다.
위의 코드의 'return balance + money' 에서 'balance + money' 을 리턴값으로 보시면 됩니다.
3) 실습
(1) 아래 코드에서 (1)번과 (2)번의 주석을 번갈아 가며 풀고 실행 시 (3)번 결과가 어떻게 나올지 예측해 보세요.
def deposit(balance, money):
print("입금이 완료되었습니다. 잔액은 {0} 원입니다.".format(balance + money))
return balance + money
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) # (1)<-------------------
# balance = withdraw(balance, 500) # (2)<---------------------
print(balance) # (3) <----------------------
(2) 아래 코드 실행결과가 어떻게 나올지 예측해 보세요.
def deposit(balance, money):
print("입금이 완료되었습니다. 잔액은 {0} 원입니다.".format(balance + money))
return balance + money
def withdraw_night(balance, money):
commission = 100
return commission, balance - money - commission
balance = 0
balance = deposit(balance, 1000)
commission, balance = withdraw_night(balance, 500)
print("수수료 {0} 원 이며, 잔액은 {1} 원입니다.".format(commission, balance))
728x90
반응형
LIST