상세 컨텐츠

본문 제목

[파이썬(Python)] #4. 함수

python

by 빨간눈동자 2021. 8. 14. 09:00

본문

반응형

python에서 함수를 정의하기 위해서는 def 키워드를 사용한다. 

# test.py
def hello_world():
	print("welcome to python world!!")
    
hello_world()

 

여러 매개변수 전달 및 리턴 값 받기 

# test.py
def calcualte(a, b):
    return a+b, a*b

sum, mul = calcualte(2, 3)
print("sum={0}, mul={1}".format(sum, mul))

 

또 다른 예제를 살펴보도록 하자 

# test.py
def calcualte(msg, x = 1, *nums, **key_nums):
    print('msg = ', msg)
    print('x = ',x)
    print('nums = ', nums)
    print('key_nums = ', key_nums)
    total = x
    for n in nums:
        total += n
    for key, value in key_nums.items():
        print("key {0}, value {1}".format(key, value))
        total += value
    return msg + " = " + str(total)
    
ret = calcualte("add", 3, 4, 5, 6, a = 1, b = 2, c = 3)
print( ret )

 

반응형

관련글 더보기