avatarJim McAulay🍁 I'm nobody. Are you a nobody too?

Summary

The provided web content discusses the concept of functions in Python, detailing how to define, call, and use a simple function within a module, emphasizing the importance of code reusability and proper indentation in Python.

Abstract

The text introduces the idea of functions in Python as a means to create reusable code blocks. It guides the reader through the process of defining a function using the def keyword, demonstrating this with a simple pr_hworld function that prints "Hello world!" when called. The necessity of calling a function to execute its code is highlighted, along with the significance of proper indentation in Python, which is mandatory for defining code blocks. The text also touches on the ability to import and use functions from one module in another, setting the stage for more detailed exploration of the import command and function creation in subsequent lessons. The article concludes with a humorous anecdote shared by Jim McAulay, which serves as a light-hearted example of miscommunication.

Opinions

  • The author emphasizes that functions are a fundamental aspect of Python for creating reusable code.
  • Proper indentation is not just a recommendation but a requirement in Python to define code blocks, which is a best practice in many programming languages.
  • The use of the def keyword to define a function is a core concept that beginners must grasp to effectively write Python code.
  • Calling a function is an explicit action required to execute the code within the function's definition.
  • The article suggests that while the example function pr_hworld is simple, it effectively demonstrates the basic mechanics of how functions operate in Python.
  • Importing modules and using their functions in other modules is presented as a powerful feature of Python, enhancing code organization and reuse.
  • The anecdote by Jim McAulay at the end of the article implies that clear communication, much like clear coding, is essential to avoid misunderstandings.

A Simple Python Function

Creating reusable code

Photo by Eiliv-Sonas Aceron on Unsplash

In a previous lesson we learned that in Python a module is just a file with Python code and we learned about modules with useful functions that came when you installed Python.

What is a function? In Python a function is a piece of reusable code.

Let’s go back to our hello_world module and rewrite it to create our first function.

A function is created with the command def. Def is short for define.

Let’s call our new function pr_hworld.

def pr_hworld():
     print ("Hello world!)

Now run the program. It doesn’t do anything.

In order to use a function, you have to call it.

Notice that when you created the module with Idle 5 spaces were added in front of the word print. This is to show that you are creating a block of code (usually more than one line). In other computer languages it is recommended best practice. In Python it is mandatory. If you don’t have the spacing it won’t run.

You can call the function pr_hworld from within the module.

def pr_hworld():
     print ("Hello world!:)
pr_hworld()

Now if you run the program it will print Hello world! This is of course, rather silly since if you wanted to print Hello World! you could just write

print ("Hello World")

But it does give you an idea of how functions work.

You can also import the module hello_world and use the function in any other module that you create.

In following lessons we are going to look at the import command and explore creating functions in a bit more detail.

Jim McAulay🍁 says, “ I called my wife from work and said that I wanted to eat out. She left a sandwich on the front doorstep.”

43–44

11–09

Python
Technology
Programming For Beginners
Illumination
Jim Mcaulay
Recommended from ReadMedium