함수를 정의하는 것은 이름만 부여하고 함수에 포함될 매개변수를 지정하며 코드 블록을 구성합니다.
함수의 기본 구조가 완성되면 다른 함수에서 호출하거나 Python 프롬프트에서 직접 호출하여 실행할 수 있습니다. 다음은 printme() 함수를 호출하는 예입니다 -
#!/usr/bin/python
# Function definition is here
def printme( str ):
"This prints a passed string into this function"
print str
return;
# Now you can call printme function
printme("I'm first call to user defined function!")
printme("Again second call to the same function") 출력
위의 코드가 실행되면 다음과 같은 결과가 생성됩니다 -
I'm first call to user defined function! Again second call to the same function