...

/

Function Creation

Function Creation

Learn how to create and use functions in Python.

Structure

How do we actually make a function? In Python, a function can be defined using the def keyword in the following format:

Press + to interact
  1. The function name is simply the name we’ll use to identify the function and call it inside our code. The naming best practices are similar to those we discussed for variable naming in previous lessons—use snake_caseUse lowercase characters and separate words using underscores., avoid uncommon abbreviationsAbbreviations that are not widely used are called uncommon abbreviations. For example, 'cfm' stands for 'cubic feet per minute,, avoid special charactersSpecial characters are symbols that are not letters or digits, used for various purposes; for example, @ in email addresses or # in hashtags., and start function names with a verb that corresponds to the action performed by the function. sort_list and calculate_area are good examples of function naming.

  2. The parameters of a function are the inputs for that function. They are variables and can be used within their function block similar to how variables are used. Parameters are optional.

  3. The body of the function contains the set of operations that the function will perform. This is always indented to the right. Similar to how we indented the code belonging to an if block.

Implementation

Let’s start by making a plain function that prints four lines of text. It won’t have any parameters. We’ll name it ...

Access this course and 1400+ top-rated courses and projects.