함수를 이용하여 특정한 동작을 하는 코드를 반복 할 수 있다.

예를 들어, 피타고라스의 정리를 수행하는 함수는 다음과 같이 정의하여 호출한다.

import math

def pythagoras(a,b):
    value = math.sqrt(a *a + b*b)
    print(value)

pythagoras(3,3)


함수의 계산 결과를 변수에 저장하여 호출하는 것도 가능하다.

import math

def pythagoras(a,b):
    value = math.sqrt(a*a + b*b)
    return value


result = pythagoras(3,3)
print(result)


'프로그래밍 > Python' 카테고리의 다른 글

Python Tutorial : Tuples [8]  (0) 2016.04.15
Python Tutorial : Lists [7]  (0) 2016.04.15
Python Tutorial : While loop [5]  (0) 2016.04.15
Python Tutorial : For loops [4]  (0) 2016.04.15
Python Tutorial : Variables [3]  (0) 2016.04.15

+ Recent posts