More on Functions Intro to Computer Science CS

  • Slides: 19
Download presentation
More on Functions Intro to Computer Science CS 1510, Section 2 Dr. Sarah Diesburg

More on Functions Intro to Computer Science CS 1510, Section 2 Dr. Sarah Diesburg

What are functions? Little pieces of code that we can write and invoke by

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

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

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

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

Procedures are functions that don’t “return” anything in any case (other than None) def

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”) 5

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

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

Functions Remember, a function stops running when it hits a return statement Often times

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. ")) is. Prime

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

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

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

prime. As. Function. py def is. Prime(number): for divisor in range(2, number): if number%divisor==0:

prime. As. Function. py def is. Prime(number): for divisor in range(2, number): if number%divisor==0: return False return True Much smaller! 10

Future Programming Assignments I will ask you to write python files with only functions

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 (F 5) At interpreter, type your function name with parameters 11

Example Take function. Fun. py and deleted all the code starting at # Running

Example Take function. Fun. py and deleted all the code starting at # Running the main part of the program Save Run the file Type >>> my. Sum = add. Num. To. Self(4) >>>print(my. Sum) 12

Revisiting Design Documents Now that our future programming assignments will involve functions, I will

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

Code Comments Still do what you are already doing with the header Before each

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 14

Example Program Let’s say that we are writing a computer rock/paper/scissors game with 4

Example Program Let’s say that we are writing a computer rock/paper/scissors game with 4 functions Function get. Weapon(): no inputs, gets random weapon from “r”, “p”, “s”, returns “r”, ”p”, ”s” Function is. Tie(one, two): inputs weapons from both players, returns true if tie and false otherwise Function player. One. Wins(one, two): inputs weapons from both players, returns true if player one wins and false otherwise 15

Example Program Let’s say that we are writing a computer rock/paper/scissors game with 4

Example Program Let’s say that we are writing a computer rock/paper/scissors game with 4 functions Function rps(number. Of. Games): input is the number of games (int), plays rock paper scissors with two computer players, and returns the result of the number of player 1 wins, player 2 wins, and ties in a string 16

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

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

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

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

Remember… What should you do first before starting to code? Figure out your algorithm

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 19