Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "More on Functions (Part 2) Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg."— Presentation transcript:

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

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

3 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

4 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

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

6 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

7 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

8 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

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

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

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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

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

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


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

Similar presentations


Ads by Google