<< Chapter < Page Chapter >> Page >

We will begin with these simple rules in this module. A future module titled Itse1359-1280-Function Arguments will get into more complicated material involving optional ways to define and use functionarguments.

The general syntax for a Python function definition is shown in Figure 1 .

Figure 1 . Syntax for a Python function definition.
def name( parameters ): """optional docstring"""codeBlock optional return [expression]

Discussion and sample code

A simple function named getRoot is defined and called in Listing 1 .

Listing 1 . A simple function named getRoot.
# Illustrates a simple function definition #-----------------------------------------def getRoot(number,root): """Returns the nth root of a number"""theRoot = number**(1/root) return theRoot#End function definition #Call the functionprint("square root of 2") print(getRoot(2,2))print("cube root of 27") print(getRoot(27,3))print("eighth root of 256") print(getRoot(256,8))print("sixteenth root of 65536") print(getRoot(65536,16))

Figure 2 shows the output produced by the code in Listing 1 .

Figure 2 . Output produced by the code in Listing 1.
square root of 2 1.4142135623730951cube root of 27 3.0eighth root of 256 2.0sixteenth root of 65536 2.0

Listing 1 defines a function that returns the nth root of a number. The first parameter specifies the number for which the root is to be obtained.The second parameter specifies which root is to be obtained.

The function definition in Listing 1 complies with each of the rules listed above .

You should have no difficulty understanding the body of the function shown in Listing 1 provided that you understand how to use exponentiation to compute a root of a number. Otherwise, you may need to dust off your old high school algebrabook and do some remedial study of algebra.

If you are content to define and use simple functions with simple argument lists as in Listing 1 , that is about all you need to know about Python functions.However, if you want to tap into the more-powerful capabilities of functions, you will need to study the module titled Itse1359-1280-Function Arguments and possibly other modules as well.

Visualizing a simple function

Figure 3 shows a visualization of the code from Listing 1 , which defines a simple function named getRoot and calls it several times.

Figure 3. Visualizing the code from Listing 1.

Visualizing the code from Listing 1

The following options were selected when performing this visualization:

  1. show exited frames (Python)
  2. render all objects on the heap
  3. draw pointers as arrows

The first item in this list of options is what caused the four light gray occurrences of the calls to the getRoot function to be visible on the right in Figure 3 . (If you were to change that option to "hide exited frames [default]", the diagram would show calls to the function while control is within the function but those calls would disappear when control leaves the function.) The pointers associated with those occurrences point to the incoming parameter values and thereturn value for each call to the getRoot function.

I recommend that you create a visualization for the code in Listing 1 and step through the program one instruction at a time. As you do that, pay attention tothe movements of the red and green arrows on the left, the diagram on the right, and the printed material at the bottom. That should help you to betterunderstand the behavior of Python programs containing functions that you define.

Run the program

I encourage you to copy the code from Listing 1 . Execute the code and confirm that you get the same results as those shown in Figure 2 . Experiment with the code, making changes, and observing the results of your changes. Make certain that youcan explain why your changes behave as they do.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Itse1359-1270-Functions
  • File: Itse1359-1270.htm
  • Published: 10/26/14
  • Revised: 01/31/16
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

-end-

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