Presentation is loading. Please wait.

Presentation is loading. Please wait.

Simple Functions and Names Sec 9-4 Web Design. Objectives The student will: Know how to create a simple function in Python. Know how to call a function.

Similar presentations


Presentation on theme: "Simple Functions and Names Sec 9-4 Web Design. Objectives The student will: Know how to create a simple function in Python. Know how to call a function."— Presentation transcript:

1 Simple Functions and Names Sec 9-4 Web Design

2 Objectives The student will: Know how to create a simple function in Python. Know how to call a function in Python. Know the rules for “names” in Python

3 Functions A function is a piece of code you can use over and over again – Treat it like a black box You pass it (optional) values, it does some work, and it (optionally) returns values You “call it”,”invoke it”, or “use it” by using its name and parentheses The things you pass it go inside the parentheses – output = function( input ) function input output

4 Functions As in any programming language Python has functions. – Functions allows you to modularize your code Easier to read Easier to debug Easy to use over and over

5 Functions The basic syntax for defining a Python function takes the form: def ( ):...

6 Functions def ( ):... Note that the lines of the function are indented. – The number of spaces that make up the indentation is not that important as long as they are all the same.

7 Improper Functions This will not be accepted by Python: Red bar shows the point of the error This is acceptable

8 Python Functions in IDLE IDLE will automatically indent lines in a function. – To end the function simple un-indent the line. IDLE also uses colors to help the readability of a program: Red – Comments Blue – Function Names Orange – Python word

9 Calling (Invoking) a Function To call a function you type the function: yoyo() forward()

10 Rules for Python Names Some names are reserved by the system and are already defined. Examples are things like: def, print, if, else, while, for, in, and, or, not, return. These names are built in keywords. A name in Python must begin with either an alphabetic letter (a-z or A-Z) or the underscore (i.e. _) and can be followed by any sequence of letters, digits, or underscore letters. For example, Vaild:Invalid: iRobot2robots myRobotRobots-r-cool jitterBug jitterBug2 my2cents my_2_cents

11 Summary Functions are defined by: def ( ):... Functions are called by FUNCTION() Function names must begin with either an alphabetic letter (a-z or A-Z) or the underscore (i.e. _) and can be followed by any sequence of letters, digits, or underscore letters.

12 Rest of Today Take the code for the octagon and put it in a function. Call the function 4 times. When the function is done show me the results.


Download ppt "Simple Functions and Names Sec 9-4 Web Design. Objectives The student will: Know how to create a simple function in Python. Know how to call a function."

Similar presentations


Ads by Google