8. Functions 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus

Slides:



Advertisements
Similar presentations
Noadswood Science,  To understand the flow procedure when writing programs Thursday, January 15, 2015.
Advertisements

 Caesar used to encrypt his messages using a very simple algorithm, which could be easily decrypted if you know the key.  He would take each letter.
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Main task -write me a program
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
CS 116 Tutorial 5 Introduction to Python. Review Basic Python Python is a series of statements def f(p1, p2,…pn): x = 5 if x > p1: x = p1 + p2 return.
Mini Project II – Drawing Machine
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.
General Programming Introduction to Computing Science and Programming I.
COMP 171: Functions, for loops and range John Barr Section 03 Slides by Toby Dragon.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
B.A. (Mahayana Studies) Introduction to Computer Science November March Logo (Part 2) More complex procedures using parameters,
A way to pull together related data A Shape Class would contain the following data: x, y, height, width Then associate methods with that data A Shape.
Oct 15, 2007Sprenkle - CS1111 Objectives Creating your own functions.
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
Writing JavaScript Functions. Goals By the end of this unit, you should understand … How to breakdown applications into individual, re-usable modules.
Vocabulary BurnishingBurnishing ~ to make something shine by rubbing it with a smooth instrument (lighter colored pencil for this assignment) Complementary.
Elementary C++. Procedural Programming Split your problem into simpler parts then solve each part separately Recognize common parts and solve them only.
1 Project designed and created by M. Shajith Kumar.
Python Let’s get started!.
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
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
CompSci 101 Introduction to Computer Science February 11, 2016 Prof. Rodger.
Introduction to: Python and OpenSesame FOR PROS. OpenSesame In OpenSesame you can add Python in-line codes which enables complex experiment. We will go.
Turtle Graphics Lesson 2 1. There are 3 homeworks to complete during the six lessons of this unit. Your teacher will let you know when a homework has.
VG101 RECITATION 5 By TAs. CONTENTS How to read Vg101Class.h Samples about graphics About assignment 5 Array.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.
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
11. Skier Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
3. Drawing Let’s Learn Saengthong School, June – August 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
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.
Functions. functions – learning targets I will be able to understand how to trace function I will be able to learn how to lavage inputs and outputs with.
4. If Statements 1 Have your program make choices. Depending on a test have a program do different things Computer Programming BSc in Digital.
5. Loops 1 Make a program do things over-and-over-and over again. Counting loops; Conditional loops. Games: guessing, ghost, hangman Computer.
Introduction to Python
Exploring Mathematical Relationships Module 5: Investigation 3
CS 1110 Introduction to Programming Spring 2017
Topic: Functions – Part 2
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
Frozen Graphics Lesson 3.
CSc 110, Spring 2017 Lecture 11: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
Follow Directions with Colors and Shapes
Let’s Learn 6. Nested Loops Saenthong School, January – February 2016
6. Lists Let's Learn Python and Pygame
Let's Learn Python and Pygame
7. Functions Computer Programming BSc in Digital Media, PSUIC
CS 1111 Introduction to Programming Fall 2018
7. Functions Let's Learn Python and Pygame
Chap. 3 Functions Start Python IDLE (Shell) and open a new file.
Introduction to Value-Returning Functions: Generating Random Numbers
functions: argument, return value
CS 1111 Introduction to Programming Spring 2019
What are variables? Using input()
CMPT 120 Lecture 4 – Unit 1 – Chatbots
Let’s Learn the Basic Colors
Using Modules.
Defining Functions.
Presentation transcript:

8. Functions 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus How to write and use functions – reusable chunks of code. A look at argument passing, return, local and global variables.

 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 1. Address Function 2 Address.py

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

Many Arguments  Even More Reuseable 4 Address2.py

 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 2. Having a Function Return Something 5 return answer

Calculating Tax 6 CalcTax-1.py

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

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

CalcTaxLocalChg-2.py 9 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 10 p is a local variable p's data is a copy of price's data p disappears at the end of the function

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

12 main program (the caller) : totalPrice = calculateTax( price, 0.06) price total Price global variables They can be seen by every function. They cannot be changed unless the function uses the special word global

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

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

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

 Python allows global variables to be changed if they are labelled as global in the function.  Avoid this kind of programming. It makes programs hard to understand. Global Variable can be Changed! 16

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

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

5. Silly Sentences 19

6. Caesar Cypher secret messages are so cool!  vhfuhw phvvdjhv duh vr frro!

# apply Caesar cipher and return the result def encrypt(message): return shiftLetters(message, 3) # undo the Caesar cipher and return the result def decrypt(message): return shiftLetters(message, -3) : caesar.py 21

# shift every letter in the word by the given number of places, # wrapping around as necessary def shiftLetters(message, shift): message = message.lower() secret = "" for c in message: if c in "abcdefghijklmnopqrstuvwxyz": num = ord(c) num += shift if num > ord("z"): # wrap if necessary num -= 26 elif num < ord("a"): num += 26 secret = secret + chr(num) else: # don't modify any non-letters in the message; # just add them as-is secret = secret + c return secret : 22

# main msg = input("Your message to encode? ") if len(msg) > 0: # wants to encrypt secret = encrypt(msg) print("The encoded message is:", secret) else: # empty message; wants to decrypt secret = input("Your message to decode? ") if len(secret) > 0: msg = decrypt(secret) print("The decoded message is:", msg) 23

 The Caesar cipher can be written on two rings.  By moving one ring, you can change the code. Varying the Cipher 24 Now 'A' --> 'N', 'B' -->'O', etc.

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

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

 A drawing helps: Lots of Magic Numbers in the Code 27 circle: start at (0,0), radius: 50 left eye: start at (60,-15), r: 10 right eye: start at (60,15), r: 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.

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

 Only the main code changes: Drawing Lots of Smileys 29 DrawSmileys.py

Execution 30

8. Make Shape Drawing Easier to Understand 31  ShapesInput.py is getting long and complicated.  It can be made easier to understand with functions.

ShapesInput.py with a Function 32

import turtle """ draw a spirograph of shapes using the angle of rotation to turn after each shape is drawn """ def drawSpiro(ttl, lineLen, numSides, rotAngle): numShapes = 360 // rotAngle for shape in range(numShapes): drawShape(ttl, lineLen, numSides) ttl.right(rotAngle) # draw a shape using the line length and number of sides def drawShape(ttl, lineLen, numSides): if ((numSides > 2) and (lineLen > 0)): angle = 360 / numSides for i in range(numSides): ttl.forward(lineLen) ttl.right(angle) : 9. Spirograph.py 33

# main t = turtle.Turtle() t.speed("fastest") rotAngle = int(turtle.numinput("Shape Rotation", "How much should each shape be rotated?")) lineLen = 100 t.color('blue'); drawSpiro(t, lineLen, 3, rotAngle); t.color('red') drawSpiro(t, lineLen, 4, rotAngle); t.color('green'); drawSpiro(t, lineLen, 5, rotAngle); t.color('orange'); drawSpiro(t, lineLen, 6, rotAngle); 34

Execution 35

10. Turtles Walking About 36

TurtlesWalk.py 37 Ten turtles are stored in a list, and each one is randomly moved and turned

11. Draw Multicolored Hearts 38

from math import * from turtle import Turtle colors = ["red", "orange", "yellow", "green", "blue", "purple", "violet"] def hearts(ttl): # draw 50 hearts in different colors ttl.speed(0) for i in range(1, 50): color(ttl, i) at(ttl, 0, i * -5) heart(ttl, i * 10, 45) def color(ttl, i): # cycle through the colors list to set the pen ttl.pencolor(colors[i % len(colors)]) Hearts.py 39 # main t = Turtle() hearts(t)

def at(ttl, x, y): # move the turtle to (x,y), pointing up screen ttl.penup() ttl.home() ttl.forward(x) ttl.left(90) ttl.forward(y) ttl.pendown() def heart(ttl, size, angle): # a heart is made from 2 arcs and 2 straight lines ttl.width(10) radius = size * sin(radians(angle))/ \ (1 + sin(radians(90-angle))) ttl.right(angle) ttl.forward(size) # line ttl.circle(radius, angle) # arc ttl.right(180) ttl.circle(radius, angle) # arc ttl.forward(size) # line ttl.left(180 - angle) 40

 Func.py contains graphing functions built using the turtle module.  The main code shows how to draw graphs for:  sine()  cosine()  a function you define; in this case y = x Use turtle for Graphs 41

42 Execution