Search code examples
pythonfunctionreturn

Why won't my custom function execute from the file editor in Python?


I recently started a Python course on Udemy. I couldn't get a grasp on the return function.

For example, in the following simple program:

def f(a, b):
    x = a + b
    return x

f(3, 3)

Simply executing this as a .py will not display the result 6

However, manually executing f(3, 3) in the console will display 6

Could I get some intuition or explanation as to what is happening here?

I'm running Python 3.8.3 on Windows 10


Solution

  • You've defined the function in the script, but have not called it anywhere.

    You need to call it and store the result in a variable.

    Your script should be like this:

    def f(a, b):
        x = a + b
        return x
    
    result = f(3,3)
    print(result)
    

    Finally you will run python a.py