Hello, just started on python and I am confused about how the return function works.

Given Task:

Define a function factorial that takes an integer x as input.

Calculate and return the factorial of that number.

My initial function ( the - represents spaces that i used in my coding, the post doesnt seem to show them ):

def factorial(x):

–product = 1

–for i in range(x):

----product = product * (i+1)

----return product

After subbing x as various numbers, i realized that the result always seems to be 1 and i am unsure why

After correction:

def factorial(x):

–product = 1

–for i in range(x):

----product = product * (i+1)

–return product

By bringing the return function out of the for function, it seemed to work.

Can someone help explain the meaning of the return function and how it led to the answer always being 1 in my initial function. Thank you!! :))