7. Functions Let's Learn Python and Pygame

Slides:



Advertisements
Similar presentations
 Functions breakdown: Functions purpose Modularity Declaring and defining a function Calling a function Parameter passing Returning a value Reusability.
Advertisements

Scope and Lifespan of Identifiers. Every function has a scope What does that mean? That means that every identifier that is created in a function (that’s.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Intro to Robots Robots are Everywhere. Intro to Robots Various robots in use today lawnmower pet baby seal for convalescents gutter cleaner home security.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
Introduction to Methods
Seminar II: Prelims/0 1 Seminar II Objective – –to give some background on the course , Semester 2, Who I am: Andrew Davison WiG.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Agenda  Perform Quiz#3 (15 Minutes)  Introduction to Pointers –What are pointers? / Why use pointers? –Pointer Terminology Memory Address format specifier.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
CPS120: Introduction to Computer Science Functions.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
ProgLan Python Session 4. Functions are a convenient way to divide your code into useful blocks, allowing us to: order our code, make it more readable,
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
Python Let’s get started!.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Chapter 6 Methods Chapter 6 - Methods.
Introduction to Functions CSIS 1595: Fundamentals of Programming and Problem Solving 1.
5. Loops 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Guide to Programming with Python Chapter Six Functions: The Tic-Tac-Toe Game.
1. Starting 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Let’s Learn 3. Modules Saenthong School, January – February 2016
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
8. Functions 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
4. If Statements 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Functions. What is a Function?  We have already used a few functions. Can you give some examples?  Some functions take a comma-separated list of arguments.
5. Animation Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
4. If Statements 1 Have your program make choices. Depending on a test have a program do different things Computer Programming BSc in Digital.
6. Classes & Objects Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
5. Loops 1 Make a program do things over-and-over-and over again. Counting loops; Conditional loops. Games: guessing, ghost, hangman Computer.
Let’s Learn 12. Packaging a Game Saengthong School, June – August 2016
A Playful Introduction to Programming by Jason R. Briggs
Let’s Learn 2. Installing Pygame
Python Let’s get started!.
Predefined Functions Revisited
Topic: Functions – Part 2
11. Animation Let's Learn Python and Pygame
Algorithm development
12. Classes & Objects Let's Learn Python and Pygame
9. Drawing Let's Learn Python and Pygame
5. Loops Let's Learn Python and Pygame
4. If Statements Let's Learn Python and Pygame
Thinking about programming
What are variables? Using input()
Let's Learn Python and Pygame
13. Sprites Let's Learn Python and Pygame
Chapter 4 void Functions
Let’s Learn 6. Nested Loops Saenthong School, January – February 2016
Programming In Lesson 3.
8. Starting Pygame Let's Learn Python and Pygame
10. User Input Let's Learn Python and Pygame
6. Lists Let's Learn Python and Pygame
Let's Learn Python and Pygame
BSc in Digital Media, PSUIC
7. Functions Computer Programming BSc in Digital Media, PSUIC
Let’s Learn 7. Sprites Saengthong School, June – August 2016
Writing Functions.
14. Pong Let's Learn Python and Pygame
What are variables? Using input()
Thinking about programming
Thinking about programming
Predefined Functions Revisited
What are variables? Using input()
Methods and Data Passing
Presentation transcript:

7. Functions Let's Learn Python and Pygame Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th 7. Functions How to write and use functions – reusable chunks of code. A look at argument passing, return, local and global variables.

1. Address Function I often need to print my address instead of writing the code many times, write the code once in a function, and reuse the function Address.py

Making the Function More Reusable Pass in data (an argument) to the function, which can change with each call. Address1.py

Many Arguments  Even More Reuseable Address2.py

2. Having a Function Return Something A function that calculates something would be very useful if it returned the answer back to the main program (the caller) use return at the end of the function return answer

Calculating Tax CalcTax-1.py

Main and Function in Pictures main program (the caller) calculateTax() (the function) integer copy data p price tax_ rate 1 : totalPrice = calculateTax( price, 0.06) total : return total 2 integer copy data total Price

3. Local Variables in a Function Local variables in a function are created when the function starts, and disappear when the function returns. calculateTax() p tax_ rate local variables local variables disappear when the function returns total : return total

CalcTaxLocalChg-2.py p is a local variable p's data is a copy of price's data p disappears at the end of the function

CalcTaxLocal-3.py p is a local variable p's data is a copy of price's data p disappears at the end of the function

4. Global Variables in a Program Global variables can be seen by every function in a program. Global variables can not be changed in a function unless you use a special word... (see later) All variables in the main program are global variables.

main program (the caller) global variables price They can be seen by every function. They cannot be changed inside a function price : totalPrice = calculateTax( price, 0.06) total Price

CalcTaxGlobalUse-4.py p, tax_rate, and total are local variables in calculateTax() price is a global variable defined in the main program

CalcTaxGlobalChg-5.py price is a global variable defined in the main program It can be seen but not changed in the function. When a change is tried, a new local variable is made instead

No Global Variable Changing calculateTax() main program (the caller) p no changing of globals allowed inside function tax_ rate price total price = 10000 When a change is tried, a local variable is made instead 10000 price total Price : return total

Global Variable can be Changed! Python allows a global variable to be changed inside a function if the variable is labelled as global in the function. Avoid this kind of programming. It makes programs hard to understand.

CalcTaxGlobalChg-6.py price is a global variable defined in the main program price is labelled as global in the function. It can now be changed inside the function.

Global Variable Changing calculateTax() main program (the caller) p tax_ rate price changing of price allowed total global price price = 10000 No local variable created : return total total Price Bad style – avoid this kind of programming

5. Silly Sentences

6. Draw a Smiley (bad coding style) DrawSmiley1.py

Horrible, but it Works... It is bad style because it is very long, with no comments to explain it. A big function should be split into smaller functions. A BIG problem is easier to solve if it is split into smaller problems.

Lots of Magic Numbers in the Code A drawing helps: circle: start at (0,0), radius: 50 left eye: start at (60,-15), r: 10 right eye: start at (60,15), r: 10 -- should be able to use one function, called two times mouth: 3 thick lines: (-25,40) --> (-10,20) (-10,20) --> (10,20) (10,20) --> (25,40) This should be in the program as comments.

7. Draw a Smiley (good style) Easier to understand Easier to change Use comments and functions DrawSmiley2.py