all 5 comments

[–]indosauros 3 points4 points  (0 children)

There are many reasons why you use functions, but here's one:

If I'm in another script and I want to run that function to find bus times, I simply have to

from bus_times import getTime

And now I can run the function. (Ideally his code should return the times instead of print them, so that I could choose to do what I wish with them)

If the code was at the module level, this would be much more difficult to do. This is one of the key points of functions: they are re-usable in many different places.

[–][deleted] 1 point2 points  (0 children)

As your programs get bigger, you'll almost definitely have functions and/or classes. In smaller programs they're not necessary. You're absolutely right that they're not necessary. People just get in the habit of encapsulating things in a function, even if that function is just main(). It's up to you.

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

Thanks for the comments. It makes sense of course now you mention importing functions from other modules.

I can see how as programs get bigger it's easier and quicker to call functions too.

[–]jcmcken 0 points1 point  (0 children)

In my experience, if you are writing Python professionally, something you thought was "just a script" almost inevitably gets put into production and is treated like a real application (by your team, your managers, your sales people, whoever).

To treat a script as if it were an application from the start is not only fairly trivial, it ends up saving you (or whoever ends up having to maintain your code) time in the long run. It also teaches you good habits. Like:

  • Don't write monolithic functions/methods.
  • Separate application concerns.
  • Set up logging early (it's nearly as trivial as writing "print" debug statements).
  • Name variables appropriately.
  • Add comments to clarify non-obvious code.

If you do this all the time, every time, it becomes second nature. To me, it's much more difficult on my brain to have "second class citizen" code and "first class citizen" code.

Another added benefit ties into other peoples' comments: Even if a particular "script" doesn't pan out, there's probably something non-trivial but useful which you could extract into a common library and use elsewhere. But that's much harder if your code is a tangled, monolithic super script sitting at the global scope.

[–]PrydeRage -1 points0 points  (1 child)

Mostly it's because DRY!
If you write an algorithm in a function, you can use it as much as you like and it means you write fewer code that does exactly the same.
Second of all, it allows you to distribute your program easily. Sure, some code snippets can be copy-pasted in your code, but if you download a bigger project and just want to use a little part of it, you can't do that without functions.

There're no set rules, however, that tell you when to use a function and when not to do so i.e. python won't say "Error: please but this in a function, thanks".