본문 바로가기
개발/python

[python] print와 return의 차이점

by 유다110 2017. 10. 27.
반응형

print와 return은 모두 function이 아닌 statement다.

뭐 이건 제쳐두고,

print와 return의 가장 큰 차이점은 함수 및 프로그램에 영향을 주느냐 마느냐이다.


print는,

그저 값을 출력할 뿐, 컴퓨터가 이 값을 가지고 무얼 하진 못한다.

변수가 어떤 값을 가지는지 사용자 측에서 편하게 보기 위함이지 함수에는 전혀 영향을 끼치지 않는다.


return은,

함수가 값을 반환하는 가장 주된 방법이다.

모든 함수는 어떠한 값을 return하며, 이 return(혹은 yield)이 명시되어 있지 않은 경우에는 None을 return한다.

이 반환된 값은 다른 함수에서 사용될 수 있으며 변수에 저장될 수도 있다.


def function_that_prints():
    print("I printed")

def function_that_returns():
    return "I returned"
	
f1 = function_that_prints()
f2 = function_that_returns()
print("Now let us see what the values of f1 and f2 are")
print(f1)
print(f2)


출력결과

"I printed"
"Lets see what the values of f1 and f2 are"    
None
"I returned"



출처: https://www.codecademy.com/ko/forum_questions/518ffbfeb3f05c44fe001395

반응형

댓글