Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

Slides:



Advertisements
Similar presentations
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
Advertisements

Introduction to Functional Decomposition Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Main task -write me a program
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.
More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
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.
More on Functions 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 Basics  Functions  Loops  Recursion. Built-in functions >>> type (32) >>> int(‘32’) 32  From math >>>import math >>> degrees = 45 >>> radians.
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.
Whatcha doin'? Aims: To start using Python. To understand loops.
Topic: Functions – Part 1
More on Functions (Part 2)
Introduction to Python
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Variables, Expressions, and IO
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
1 Python Lab #1 Intro to Python Adriane Huber Debbie Bartlett.
Thinking about programming
Introduction to Functional Decomposition
Writing Functions( ) (Part 5)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
More Nested Loops and Lab 5
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Starter 15//2 = 7 (Quotient or Floor) (Modulus) 22%3 =1
Lists – Indexing and Sorting
Intro to Nested Looping
Programming In Lesson 3.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
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)
Python: Algorithms Damian Gordon.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
More on Functions (Part 2)
More on Functions (Part 2)
Lists – Indexing and Sorting
Intro to Nested Looping
A look at Python Programming Language 2018.
Python programming exercise
Python 19 Mr. Husch.
Types, Truth, and Expressions (Part 2)
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Big problem  small steps
Console.WriteLine(“Good luck!”);
Thinking about programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Thinking about programming
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Intro to Nested Looping
Lists – Indexing and Sorting
Types, Truth, and Expressions
General Computer Science for Engineers CISC 106 Lecture 03
Python 19 Mr. Husch.
Pseudo-Code Conditional Branches
Functions Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Types, Truth, and Expressions (Part 2)
Functions and Abstraction
Presentation transcript:

Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg More on Functions 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

Functions can call each other! Let’s take a look at the example functionFun.py

Procedures Procedures are functions that don’t “return” anything in any case (other than None) def print_directions(): print(“Welcome to rock, paper, scissors”) print(“Enter r for rock”) print(“Enter p for paper”) print(“Enter s for scissors”) print(“Enter q to quit”)

Print Is print() a “procedure”? How can we find out?

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!

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

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)

Revisiting Design Documents Now that our future programming assignments will involve functions, I will expect comments in your code

Code Comments Still do what you are already doing with the header Before each function, list Function name Inputs – inputs parameters to your function and their types Output – what your function returns and its type Inside the function using a docstring (triple quotes) Description – what your function does

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

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

Example Take a look at RPS.py to see what I am expecting.

Remember… What should you do first before starting to code?

Remember… What should you do first before starting to code? Figure out your algorithm Write it down, draw a diagram Write your function comments So you know what your function will do before you start