A Few Python Basics to Learn from a Simple Function - Montana Webmaster

A Few Python Basics to Learn from a Simple Function

A Piece of Python Code

def multiply_by_5(x):
return 5 * x

What Is def in Python?

  1. def tells Python that you are making a function.
  2. Photo of potato masher
    A function is a software tool that does one task, like this kitchen tool that is only good for mashing.

    A function is a reusable bit of code that does one specific task, like the mashing tool I used yesterday to squish strawberries.

  3. Python will store the function with other functions, like kitchen tools in a drawer, but more organized.
  4. Most widely-used programming languages have functions. If someone is talking about “methods”, instead of using the word function, they are probably a Java developer.

Naming a Function in Python: multiply_by_5()

The next part of the code is the function name: multiply_by_5(). Like file names, functions generally have a name*. The name allow a reference to the function to be stored so it can be called over and over from one set of code. In a sense, that is what makes the code a tool.

One of the ways you can tell what is a function in the code of most language is () after the name. It is what makes a function name different from a variable name.

“Valid names start with a letter or underscore but can include numbers. Words are lowercase and separated by underscores. It’s important to know that function names can’t be a Python reserved keyword.” Programmers who are used to other languages tend to find this pattern annoying.

* The exception is lambda functions, which don’t have names because they are generally a one-use function.

What Are the Parenthesis for?

Whatever is inside the parenthesis is the potatoes, or strawberries, in the bowl. Just like it would be silly to mash away at nothing in the bowl, it would be silly to try to multiply nothing by 5. What’s in the parenthesis is a “passed in value”: the input! Another function (the calling function) gathers the input from somewhere and passes it to this function for processing.

One way that we tell the difference between a function and a variable is that a function has parenthesis after the name. A variable can be inside parenthesis, but does not have them after the name!

Design Error: this is a simple example that is not meant to show the full programming logic, but if the calling function sent in the letter “n” instead of of a number, the whole thing would crash! This would be like someone trying to mash blocks of wood instead of strawberries or potatoes … the function just can’t handle it.

Python Uses Line Breaks instead of Curly Braces { }

If you are familiar with other programming languages, you may have see functions like this:

FunctionName()
{

}

Or the opening curly brace may be at the end of the first line.

Python uses a colon to indicate the start of the code for a function and indented lines to identify the lines of code inside a function. These indents are very fussy. It is common to use 4 spaces, but as long as you use the same number every time, Python doesn’t care.

 

Resources

~ https://www.freecodecamp.org/news/python-function-examples-how-to-declare-and-invoke-with-parameters-2/

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.