#파이썬 함수 : 특정 기능을 실행하는 코드

#내장 함수
'''
print(), input()
len()
sum()
max(), min()
str(), int(), float()
'''

#사용자 정의 함수
'''
def 이름(매개변수):
    함수 내용
'''
def hello():
    print('안녕하세요')

hello()
hello()

# 인자가 있는 함수
def fruits(a):
    print('내가 좋아하는 과일은', a)

fruits('사과')
fruits('포도')

#기본값이 있는 인자
def fruits2(b = '수박'):
    print('좋아하는 과일은', b)

fruits2('사과')
fruits2()

#반환값이 있는 함수
def hi(name):
    print(f'이름은 {name}입니다')
    return name

hi('주현')
print(hi('주현'))
x = hi('민지')
print(x)

#반환값이 여러개인 함수
def add(a, b, c):
    x = a + b
    y = b + c
    return x, y

add(1, 2, 3)
print(add(1, 2, 3))

print('----문제1----')
def clac(a, b):
    A = a + b
    B = a * b
    print(A, B)
    return A, B

print(clac(7, 2))


print('----지역/전역 변수----')
#전역변수 global : 함수 안과 밖에서 모두 사용가능한 변수
def A():
    print(name + '아 안녕')
    
name = '주현'

print(name)
A()

#지역변수 local : 함수 안에서만 사용가능한 변수
def B():
    b = 1 #지역변수
    print(b)

B()
# print(b)

c = 5 #전역변수
print(c) #5
def C():
    c = 3 #지역변수
    print(c) #3

C() 
print(c) #5


def D():
    global d
    d = 7
    print(d)

D()
print(d)

print('----문제2----')
x = 10 #전역
def show():
    global y
    x = 1 #지역
    y = 5 #지역
    print(x) # 1
    print(y) # 5

show()
print(x) # 10
print(y) # 오류














Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: