Home

How to Name Functions in Python

|
|  Updated:  
2016-03-26 7:16:04
Python Essentials For Dummies
Explore Book
Buy On Amazon

Functions are extremely useful and powerful tools in your programming toolbox because they allow you to separate your program into meaningful blocks. All the built-ins in Python are functions, as is everything in the standard library.

The rules for naming a function are a lot like rules for naming a variable:

  • They must start with a letter or an underscore: _.

  • They should be lowercase.

  • They can have numbers.

  • They can be any length (within reason), but keep them short.

  • They can't be the same as a Python keyword. They can have the same name as an existing function (including a built-in), but avoid this for now.

The function print_hello_world is very basic. It only does one thing. It can't respond to different circumstances because no information is passing into the function when you call it.

This is fine if you need to do the same thing all the time. Functions are even more powerful, though, because you can communicate with them and they can do different things depending on the information. You could change this function and send it different messages to print.

About This Article

This article is from the book: 

About the book author:

Brendan Scott is a dad who loves Python and wants kids to get some of its magic too. He started pythonforkids.brendanscott.com to help teach his oldest child to code. He maintains it to help other young people learn Python.