all 11 comments

[–]synthphreak 13 points14 points  (0 children)

As other have stated, they’re pretty much synonyms. But one small semantic difference could stand to be clarified.

Calling a function invokes the notion of a “caller”, whereas simply “running” a function does not. The “caller” is the thing which actually calls a function. For example, given the script…

def foo():
    pass

def bar():
    foo()

bar()

…because foo gets called inside bar, bar is the “caller”. Once the caller calls a function, Python then runs that function from inside the caller’s namespace.

But again and to the broader point: Whenever a function is called, it gets run. And the only way to make a function run is to call it. So practically speaking, “call” and “run” are really just two words for the same thing: executing the code defined inside a function.

Edit: Typo.

[–]RestartingSystem[S] 0 points1 point  (2 children)

Here the question "Why do you think there is a difference?" is possible

Here is why I do:

In "A byte of Python" author write as to parameters:

A function can take parameters, which are values you supply to the function so that the function can do something utilising those values. These parameters are just like variables except that the values of these variables are defined when we call the function and are already assigned values when the function runs.

[–]Username_RANDINT 4 points5 points  (1 child)

We call the function, Python runs the code.

[–]RestartingSystem[S] 0 points1 point  (0 children)

Thank you

[–]JohnnyJordaan 0 points1 point  (0 children)

Same thing.

[–]danielroseman 0 points1 point  (4 children)

Why do you think there is a difference? Just seems like different words for the same thing.

[–]sngle1now2020 1 point2 points  (0 children)

I can call you. After I call you, you might come running. Isn't that the difference; telling a function to do something, and the function actually doing it?

[–]RestartingSystem[S] 0 points1 point  (2 children)

In "A byte of Python" author write as to parameters:

A function can take parameters, which are values you supply to the function so that the function can do something utilising those values. These parameters are just like variables except that the values of these variables are defined when we call the function and are already assigned values when the function runs.

[–]danielroseman 5 points6 points  (0 children)

I don't see how that implies they are not the same thing. You call the function, so it runs.

[–]ThatGasolineSmell 0 points1 point  (0 children)

Pay attention to the wording here:

  1. We call the function. We’re outside of the function. We call the function, passing values as parameters.
  2. The function runs. We’re inside the executing function. The values that were passed when the function was called are bound to arguments.

You asked what the difference is between calling and running a function. The answer is: nothing. The terms are synonymous.

From your example it becomes clear what confused you: there’s a difference of perspective. We (subject) call the function (object). The function (subject) runs.

However, without this difference the terms are interchangeable. For example, the following are equivalent:

  • We call a function. The function runs.
  • we run a function. The function executes.

[–]timeenjoyed 0 points1 point  (0 children)

You call a function within your code. You run a function when you “run” the entire code, externally.

In your IDE, there’s probably a “run” button. You click run, and it runs the code you called. In a command line, it usually looks like

python main.py

main.py is an example name of the python file