<< Chapter < Page Chapter >> Page >
This module explains the basics of defining and calling functions in Python.

Table of contents

Preface

This module is one in a collection of modules on Python designed for teaching ITSE 1359 Introduction to Scripting Languages: Python at Austin Community College in Austin, TX.

What you have learned

So far under the general topic of control flow , you have learned how to construct while loops, for loops, and if statements. In addition, you have learned

  • how to create nested loop structures ,
  • how to add an else clause to while loops and for loops, and
  • how to use the loop modifiers: continue and break .

What you will learn

This module will depart significantly from the above. In this module, you will learn the basics of defining and calling functions in Python.

Viewing tip

I recommend that you open another copy of this module in a separate browser window and use the following links to easily find and view the Figuresand the Listings while you are reading about them.

(Note to blind and visually impaired students: most of the Figures and all of the Listings inthis module are presented in plain text format and should be accessible using an audio screen reader or a braille display. Note however that the requiredindentation may not be properly represented by an audio screen reader.)

Figures

  • Figure 1 . Syntax for a Python function definition.
  • Figure 2 . Output produced by the code in Listing 1.
  • Figure 3 . Visualizing the code from Listing 1.

Listings

  • Listing 1 . A simple function named getRoot.

General background information

According to tutorialspoint -- Python Functions

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. As you already know, Python gives you many built-in functions like print(), etc. but you can also create your own functions. These functions are called user-defined functions.

Again according to tutorialspoint , the rules for defining a basic function are as follows:

  • Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
  • Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
  • The first statement of a function can be an optional statement - the documentation string of the function or docstring .
  • The code block within every function starts with a colon (:) and is indented.
  • The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

The docstring mentioned above is a string literal. We will ignore the docstring in this module. We will learn about tools in a future module that use docstrings to automatically produce online or printed documentation.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Itse 1359 introduction to scripting languages: python. OpenStax CNX. Jan 22, 2016 Download for free at https://legacy.cnx.org/content/col11713/1.32
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Itse 1359 introduction to scripting languages: python' conversation and receive update notifications?

Ask