<< 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-

Questions & Answers

Ayele, K., 2003. Introductory Economics, 3rd ed., Addis Ababa.
Widad Reply
can you send the book attached ?
Ariel
?
Ariel
What is economics
Widad Reply
the study of how humans make choices under conditions of scarcity
AI-Robot
U(x,y) = (x×y)1/2 find mu of x for y
Desalegn Reply
U(x,y) = (x×y)1/2 find mu of x for y
Desalegn
what is ecnomics
Jan Reply
this is the study of how the society manages it's scarce resources
Belonwu
what is macroeconomic
John Reply
macroeconomic is the branch of economics which studies actions, scale, activities and behaviour of the aggregate economy as a whole.
husaini
etc
husaini
difference between firm and industry
husaini Reply
what's the difference between a firm and an industry
Abdul
firm is the unit which transform inputs to output where as industry contain combination of firms with similar production 😅😅
Abdulraufu
Suppose the demand function that a firm faces shifted from Qd  120 3P to Qd  90  3P and the supply function has shifted from QS  20  2P to QS 10  2P . a) Find the effect of this change on price and quantity. b) Which of the changes in demand and supply is higher?
Toofiq Reply
explain standard reason why economic is a science
innocent Reply
factors influencing supply
Petrus Reply
what is economic.
Milan Reply
scares means__________________ends resources. unlimited
Jan
economics is a science that studies human behaviour as a relationship b/w ends and scares means which have alternative uses
Jan
calculate the profit maximizing for demand and supply
Zarshad Reply
Why qualify 28 supplies
Milan
what are explicit costs
Nomsa Reply
out-of-pocket costs for a firm, for example, payments for wages and salaries, rent, or materials
AI-Robot
concepts of supply in microeconomics
David Reply
economic overview notes
Amahle Reply
identify a demand and a supply curve
Salome Reply
i don't know
Parul
there's a difference
Aryan
Demand curve shows that how supply and others conditions affect on demand of a particular thing and what percent demand increase whith increase of supply of goods
Israr
Hi Sir please how do u calculate Cross elastic demand and income elastic demand?
Abari
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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