7. Functions Computer Programming BSc in Digital Media, PSUIC

Slides:



Advertisements
Similar presentations
COMPSCI 105 S Principles of Computer Science
Advertisements

Bellwork Find the geometric mean of 5 & 18 Find the geometric mean of 5 & 18 Find the geometric mean of 3 & 44 Find the geometric mean of 3 & 44 Solve.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
Mini Project II – Drawing Machine
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Graphics and Procedures Programming Right from the Start with Visual Basic.NET 1/e 5.
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.
Art 321 Lecture 7 Dr. J. Parker. Programming In order to ‘make things happen’ on a computer, you really have to program it. Programming is not hard and.
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Is this a square or a pentagon? It is a square.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
Vocabulary BurnishingBurnishing ~ to make something shine by rubbing it with a smooth instrument (lighter colored pencil for this assignment) Complementary.
Introduction to Computer Programming
If you say 8 color the ones in your picture purple. If you say 9 color the ones in your picture blue.
1 Project designed and created by M. Shajith Kumar.
FRACTIONS & SHAPES BY:. How many of these are colored red? * out of *.
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
Let’s Learn 3. Modules Saenthong School, January – February 2016
8. Functions 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
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.
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.
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. 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.
Using the Python Turtle
Pixels, Colors and Shapes
COLORS.
Sorting and Grouping.
Name: Spelling: 3 x each 1-5 Math: Lesson 6 Problem Set 1-8
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Design Elements and Principles. Part 1.
Topic: Functions – Part 2
11. Animation Let's Learn Python and Pygame
9. Drawing Let's Learn Python and Pygame
Butterfly Maths Each caterpillar must be coloured the correct pattern for it to turn into a butterfly. Work out each problem to know how to colour each.
5. Loops Let's Learn Python and Pygame
The Circle Isosceles Triangles in Circles
What are variables? Using input()
Eyes, Eyes, What do you See?
13. Sprites Let's Learn Python and Pygame
Follow Directions with Colors and Shapes
Let’s Learn 6. Nested Loops Saenthong School, January – February 2016
Name: _______________________________
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
6. Lists Let's Learn Python and Pygame
Let's Learn Python and Pygame
BSc in Digital Media, PSUIC
COLORS AND SHAPES BY: ALYSSA EWERS.
7. Functions Let's Learn Python and Pygame
Chap. 3 Functions Start Python IDLE (Shell) and open a new file.
Let’s Learn 7. Sprites Saengthong School, June – August 2016
3.1 “I Can draw circles, and identify and determine relationships among the radius, diameter, center, and circumference.”
14. Pong Let's Learn Python and Pygame
5. Functions Rocky K. C. Chang 30 October 2018
What are variables? Using input()
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Value-Returning Functions: Generating Random Numbers
What Color is it?.
3.1 “I Can draw circles, and identify and determine relationships among the radius, diameter, center, and circumference.”
Class 6 using the math library
What are variables? Using input()
Instructions for Cell Cycle Foldable
Let’s Learn the Basic Colors
Defining Functions.
Presentation transcript:

7. Functions 142-253 Computer Programming BSc in Digital Media, PSUIC 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 (immutable) copy data p price tax_ rate 1 : totalPrice = calculateTax( price, 0.06) total : return total 2 integer (immutable) 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

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

Execution

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

ShapesInput.py with a Function

9. Spirograph.py 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) :

# main t = turtle. Turtle() t. speed("fastest") rotAngle = int(turtle # 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);

Execution

10. Turtles Walking About

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

11. Draw Multicolored Hearts

Hearts.py # main t = Turtle() hearts(t) 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)])

def at(ttl, x, y): # move the turtle to (x,y), pointing up screen ttl 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, 180 + angle) # arc ttl.right(180) ttl.left(180 - angle)