๋ชฉ์ฐจ
์ด๋ฒ์ ํจ์ 2๋ฒ์งธ ์ด์ผ๊ธฐ
๋งค๊ฐ๋ณ์&์ธ์
๋งค๊ฐ๋ณ์(parameter)์ ์ธ์(argument)๋ ์์ฃผ ๋น์ทํด์ ํท๊ฐ๋ฆฌ๊ธฐ ์ฝ๋ค!
def func(a,b):
return a+b
v = func(x,y)
์์ ์์์์
๋งค๊ฐ๋ณ์(parameter)๋ a,b / ์ธ๋ถ์์ ๋ค์ด์จ ๊ฐ์ ๋ฐ๋ ๋ณ์์ด๋ค.
์ธ์(argument)๋ x,y / ํจ์๋ฅผ ํธ์ถํ ๋ ์ ๋ฌํ๋ ์ ๋ ฅ๊ฐ์ด๋ค.
์ด๋ ๊ฒ ์ ๋ฆฌํ ์ ์๋ค!
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
๊ธฐ๋ณธ๊ฐ
๊ธฐ๋ณธ๊ฐ์ด๋ ๋งค๊ฐ๋ณ์์ ์ด๊ธฐ ์ค์ ํด์ค ๊ฐ์ ์ด์ผ๊ธฐํ๋ค.
def profile(name, age, team = "ManUtd"):
| print(f"์ด๋ฆ : {name} | age : {age} | team = {team}")
์ ์์๋ name, age, team 3๊ฐ์ ๋งค๊ฐ๋ณ์ ์ค team๋ณ์์ ๋ฏธ๋ฆฌ ์ด๊ธฐ๊ฐ์ ์ค์ ํด์ค ๊ฒ์ด๋ค.
๋ง์ฝ team์ ์ ๋ ฅ๊ฐ์ ์ฃผ์ง ์์ผ๋ฉด ์๋์ผ๋ก ์ฌ์ ์ค์ ๊ฐ์ด ๋ค์ด๊ฐ๋ค.
profile("๋ธ๋ฃจ๋
ธ ํ๋ฅด๋๋ฐ์ค",27)
profile("๋ธ๋ฃจ๋
ธ ํ๋ฅด๋๋ฐ์ค",27,"ManUtd")
ใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
ก
์ด๋ฆ : ๋ธ๋ฃจ๋
ธ ํ๋ฅด๋๋ฐ์ค | age : 27 | team = ManUtd
์ด๋ฆ : ๋ธ๋ฃจ๋
ธ ํ๋ฅด๋๋ฐ์ค | age : 27 | team = ManUtd
์ด ๋๊ฐ์ง ๋ฐฉ๋ฒ์ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํด์ค๋ค.
์ฃผ์ํด์ผ ํ ์
def profile(team="ManUtd", name, age):
| print(f"์ด๋ฆ : {name} | age : {age} | team = {team}")
์ด๋ฒ ์์๋ ์ ๊ณผ ๋ค๋ฅธ ์ ์ด ์๋ค. ๋ฐ๋ก ๊ธฐ๋ณธ๊ฐ์ ๋ถ์ฌํ ๋งค๊ฐ๋ณ์์ ์์น์ด๋ค.
์ด ํจ์๋ฅผ ์ฌ์ฉํด ๋ณด๋ฉด?
profile("๋ธ๋ฃจ๋
ธ ํ๋ฅด๋๋ฐ์ค",27)
ใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
ก
SyntaxError: non-default argument follows default argument
์ง ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค!
์ค๋ฅ ๋ด์ฉ์ '๊ธฐ๋ณธ๊ฐX์ธ์๊ฐ ๊ธฐ๋ณธ๊ฐO์ธ์์ ๋ค์ ์๋ค!'๋ผ๋ ๊ฒ์ด๋ค.
์ฆ ๊ธฐ๋ณธ๊ฐ์ ๋ถ์ฌํ๊ณ ์ถ์ ๋งค๊ฐ๋ณ์๋ ํญ์ ๋ค์ชฝ์ผ๋ก ์์ผ ํ๋ค!
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
๋ฆฌํด๊ฐ
- ํจ์์ ๋ฆฌํด๊ฐ์ ํญ์ ํ๋์ด๋ค!
def num(a,b):
return a+b, a-b
print(num(2,1))
ใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
ก
(3,1)
ํจ์๋ฅผ ์ถ๋ ฅํด๋ณด๋ฉด ํํ ํํ์ ํ๋์ ๊ฐ์ ๋ฐํํ๋ ๊ฒ์ ์ ์ ์๋ค.
x,y = num(2,1)
print(x, y)
ใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
ก
3 1
์ด๋ฐ ์์ผ๋ก ๊ฐ์ ํ๋์ฉ ํ ๋นํ๋ ๋ฐฉ๋ฒ๋ ์ฌ์ฉํ ์ ์๋ค.
- ํจ์๋ return์ ๋ง๋จ๊ณผ ๋์์ ํ์ถํ๋ค!
def num(a,b):
return a+b
return a-b
print(num(2,1))
ใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
ก
3
์์ ์์๋ก ์ ์ ์๋ฏ ๋ ๋ฒ์งธ return์ ๊ฒฐ๊ณผ๋ ๋ฐํํ์ง ์๋๋ค.
(์ด๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ!)
def pick(champ):
if champ == 'ํฐ๋ชจ':
return
print(f"์ ์ด๋ฒ ํ์ {champ}๋ฅผ ํด๋ณด๊ฒ ์ต๋๋ค.")
pick('ํฐ๋ชจ')
pick('์ด๋ ๋ฆฌ์')
ใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
ก
์ ์ด๋ฒ ํ์ ์ด๋ ๋ฆฌ์๋ฅผ ํด๋ณด๊ฒ ์ต๋๋ค.
์ ์ฒ๋ผ ์ด๋ค ํน์ ๊ฐ์ ๋ง๋ฌ์ ๋ ๋ฐ๋ก ํจ์๋ฅผ ํ์ถํ๋ ๋ฐฉ๋ฒ์ ํ์ฉํ๊ธฐ๋ ํ๋ค!
ใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ กใ ก
lamda
ํจ์๋ฅผ ์์ฑํ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ํ๋ ๋ ์๋ค. ๋ฐ๋ก lamda์ด๋ค.
์ด๊ฒ์ ๋ณดํต def๋ฅผ ์ธ ์ ๋๋ก ๋ณต์กํ์ง ์๊ฑฐ๋ def๋ฅผ ์ฌ์ฉํ์ง ๋ชปํ๋ ๊ณณ์์ ์ฐ์ธ๋ค๊ณ ํ๋ค.
์ฌ์ฉ๋ฒ)
lamda ๋งค๊ฐ๋ณ์1, ๋งค๊ฐ๋ณ์2, ... : ๋งค๊ฐ๋ณ์ ์ด์ฉ ํํ์
sum = lamda a, b: a+b
result = sum(1,2)
print(result)
ใ
กใ
กใ
กใ
กใ
กใ
กใ
กใ
ก
3
'๐ | Python > ํ์ด์ฌ ๋ฌธ๋ฒ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ผ ์ ์ถ๋ ฅ (0) | 2021.09.23 |
---|---|
๋ณ์์ ์ ํจ๋ฒ์ (0) | 2021.09.22 |
ํจ์_(1) (0) | 2021.09.19 |
๋ฌธ์์ด ํฌ๋งคํ (0) | 2021.09.18 |
์์ ๋ณต์ฌ์ ๊น์ ๋ณต์ฌ (0) | 2021.09.13 |
๋๊ธ