More on Functions (Part 2)

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

Introduction to Functional Decomposition Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
In Class Exercises. Chapter 3, problem 25  Develop an application to compute the total cost of an order for MyJava Coffee Outlet including the boxes.
Lecture 7 Sept 17 Goals: Complete Chapter 4 Chapters 5 and 6.
Python quick start guide
Introduction to Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
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.
CPTR 124 Review for Test 1. Development Tools Editor Similar to a word processor Allows programmer to compose/save/edit source code Compiler/interpreter.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Lab 07: Caesar Cypher Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Intro to Nested Looping Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Exam Prep and Wrap-Up Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CS 100 Introduction to Computing Seminar October 7, 2015.
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
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: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
More on Functions Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
More on Functions (Part 2)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CompSci 101 Introduction to Computer Science
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Introduction to Functional Decomposition
More Repetition While and For Loops Sentinel-Controlled Loops
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
More Nested Loops and Lab 5
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CompSci 101 Introduction to Computer Science
Lists – Indexing and Sorting
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
String Encodings and Penny Math
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)
Structured Programming Taken from notes by Dr. Neil Moore
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Python: Algorithms Damian Gordon.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
More on Functions (Part 2)
Lists – Indexing and Sorting
Intro to Nested Looping
Types, Truth, and Expressions (Part 2)
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Functions and Recursion
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Nested Looping
Lists – Indexing and Sorting
String Encodings and Penny Math
Types, Truth, and Expressions
Types, Truth, and Expressions (Part 2)
Python Reserved Words Poster
More Functional Decomposition
Types, Truth, and Expressions
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

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

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

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

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

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

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

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

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

Lab 9 Questions?

Exam #2 Same format as Exam #1 Over Chapters 3-5 Does not include functions Study guide now available