More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Order of operators: x ** y x * y, x / y, x // y, x % y x + y, x - y
Introduction to Functional Decomposition Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
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.
An Introduction to Textual Programming
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Python
Python: Modularisation Damian Gordon. Modularisation Remember the prime checker program:
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Python Lesson Week 01. What we will accomplish today Install Python on your computer Using IDLE interactively to explore String Variables if/else while.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Python Programming Using Variables and input. Objectives We’re learning to build functions and to use inputs and outputs. Outcomes Build a function Use.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
1 Computer Science of Graphics and Games MONT 105S, Spring 2009 Session 1 Simple Python Programs Using Print, Variables, Input.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Python Let’s get started!.
Programming Seminar 2009 Night 0. Why we’re holding this seminar You’re probably not a computer science major – Or even anything remotely close (e.g.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
Prime Numbers Damian Gordon. Prime Numbers So let’s say we want to express the following algorithm: – Read in a number and check if it’s a prime number.
Python Basics  Values, Types, Variables, Expressions  Assignments  I/O  Control Structures.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
COSC 1223 Computer Science Concepts I Joe Bryan. What is a function? Like a mini-program that performs a specific task Two types: Built-in functions Functions.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Design of Rock-Paper-Scissors end Print instructions Play game Ask user If (s)he wants to play again Play again? Y Print stats: wins, losses and ties start.
CSC 1010 Programming for All Lecture 5 Functions Some material based on material from Marty Stepp, Instructor, University of Washington.
Whatcha doin'? Aims: To start using Python. To understand loops.
More on Functions (Part 2)
More Functional Decomposition
Introduction to Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Variables, Expressions, and IO
Introduction to Functional Decomposition
Writing Functions( ) (Part 5)
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Programming In Lesson 3.
More Functional Decomposition
Types, Truth, and Expressions (Part 2)
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
More Functional Decomposition
More on Functions (Part 2)
More on Functions (Part 2)
Intro to Nested Looping
Python programming exercise
Python 19 Mr. Husch.
Types, Truth, and Expressions (Part 2)
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Console.WriteLine(“Good luck!”);
Thinking about programming
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Thinking about programming
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Nested Looping
More Functional Decomposition
Lists – Indexing and Sorting
Types, Truth, and Expressions
Python 19 Mr. Husch.
Types, Truth, and Expressions (Part 2)
Presentation transcript:

More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

What are functions? Little pieces of code that we can write and invoke by name  Reusable  General 2

Functions Take in any number of parameters  Including no parameters! Possibly compute something Return a value  Can be None  If no return is specified, returns None 3

Function that takes in no parameters def answerToEverything(): return 42 This function takes in no parameters It does no computation Just returns a number 4

Function that returns nothing Let’s take a look at print() >>> result = print(“hi”) >>> print(result) None >>> 5

None None is a special value in Python that represents nothing  The first letter of None must be capitalized – it will turn orange Use it when you have nothing to return  Like if one of the parameters was invalid 6

Functions Remember, a function stops running when it hits a return statement Often times it is easier to write something as a function than as a script by itself Let’s look at our old script to find a prime number 7

prime.py number = int(input("Please enter a number greater than 2. ")) isPrime = True #break out condition divisor = 2 while divisor<number and isPrime: if number%divisor==0: print("The number",number,"is divisible by",\ divisor) isPrime=False # Going to break out divisor=divisor+1 8

prime.py (continued) if isPrime: print("That number is prime") else: print("That number is NOT prime") 9

primeAsFunction.py def isPrime(number): for divisor in range(2,number): if number%divisor==0: return False return True Much smaller! 10

Functions can also call themselves One function can invoke another function Let’s take a look at functionFun.py 11

Future Programming Assignments I will ask you to write python files with only functions in them That’s ok – you can call functions directly from IDLE  Step 1: Run your python file (F5)  At interpreter, type your function name with parameters 12

Example Take functionFun.py and deleted all the code starting at # Running the main part of the program Save Run the file Type >>> mySum = addNumToSelf(4) >>>print(mySum) 13

Revisiting Design Documents Now that our future programming assignments will involve functions, I will expect more from your design documents and comments in your code 14

Design Documents Your design document should have the following sections  Small “users manual” General Description – Tell me generally what the program does in a sentence or two How to run program – Tell me how to run the program, including types of inputs and outputs expected  New design document Paragraph - Tell me how your program works, broken down into major steps 15

Code Comments Still do what you are already doing Before each function, list  Function name  Inputs – inputs parameters to your function and their types  Output – what your function returns and its type  Description – what your function does 16

Example Program Let’s say that we are writing a computer- computer rock/paper/scissors game with 4 functions  Function getWeapon(): no inputs, gets random weapon from “r”, “p”, “s”, returns “r”,”p”,”s”  Function isTie(one,two): inputs weapons from both players, returns true if tie and false otherwise  Function playerOneWins(one,two): inputs weapons from both players, returns true if player one wins and false otherwise 17

Example Program Let’s say that we are writing a computer- computer rock/paper/scissors game with 4 functions  Function rps(numberOfGames): input is the number of games (int), plays rock paper scissors with two computer players, and returns the result of the number of player1 wins, player2 wins, and ties in a string 18

Remember… Write your design document and function comments first  Then fill in the code 19

Example Take a look at RPS.py and DesignOfRPS.txt to see what I am expecting. 20