1 of 16

Functions pt 2

CSCI100: Intro to computer science

2 of 16

Announcements and Reminders

  • Lab 4 is due on Friday, October 4th @ 11:59pm

  • The second Google Careers in Tech Chat is this Thursday @ 6pm (again virtual, link on the course website).
    • This week’s topic is on Product/Project Management!
  • Lecture on Monday (10/7) will be virtual. Be on the lookout for the zoom link on the course website

3 of 16

  • Previous Lesson Review

4 of 16

A little more on functions

5 of 16

Remember that functions are reusable blocks of code

  • With functions, code becomes a lot more readable
    • They allow us to break complex code to more manageable chunks
  • They can reduce errors too
    • Since functions are reusable, we lessen the chance of introducing errors by duplicating code
  • They save time and effort
    • Since we can use blocks of code multiple times, we don’t need to write them multiple times

6 of 16

Return Values

  • Not only can functions do a task, it can also give us the result of said task.
  • This is known as returning a value
  • The returned value can be stored in a variable for later use, or be printed out
  • You can return anything, from strings, to integers, to even nothing (aka None!)
  • To make a return statement, simply have the the word return followed by whatever you want to return (you can also have nothing after return, which will just return None)

def function_name():# function definition

# some code

return # something or nothing

7 of 16

Let’s look at the addition example we had Monday

Create a function that asks the user to enter two numbers, then adds the two numbers together

If I were to print the function call, it would print None because the function doesn’t return anything. How can I return the sum instead of printing it?

8 of 16

Return Values

  • When a function reaches a return statement, the function stops (think of it like a break statement, but for functions). Any code that comes after a return statement won’t be ran
  • You can only return ONE value.

9 of 16

Let’s do a little practice

Write a function that takes in a the length and width of a 4-sided polygon.

If it is a square, return the area. If it’s a rectangle, return the perimeter

EX: length: 6, width: 10, result: 32

10 of 16

Now it’s your turn to practice!

  • Break into to groups of 2. Feel free to grab some scratch paper
  • Take a look at the following problem:
    • Write a function called is_harshad that takes in an integer and returns whether or not it’s a Harshad number. A Harshad number is any number that is divisible by the sum of its digits. Return True if so, and False if not
      • Ex: is_harshad(21) → True
      • Ex: s_harshad(672) → False
  • WITHOUT WRITING ANY CODE, work with your partner to see how you can go about solving the problem step by step
    • Some questions to consider:
      • How can I get the individual digits in a number?
        • Hint: remember we can’t iterate through

Integers, so how can we work around that?

      • What are some shorter examples I can start with?
      • Are there edge cases I should consider?

11 of 16

Now it’s your turn to practice!

  • Now that you have an algorithm, have one person be the driver (the one writing the code) and one person be the navigator (telling the person what to write)
    • Write your program in ps1.py in the Functions notebook in Codio
  • Make sure you test your code periodically in the terminal!!!
  • Remember you can look up syntax or ask me. I’ll come around to check on you!

12 of 16

Scope

  • Understanding scope is extremely important in writing functions and understanding control flow
  • The scope of a variable is where a variable is created and can be used.
  • Write the following program on your computer and run it. What happens?

13 of 16

Scope

  • It’ gives an error! This is because a variable created within a function only exists within the function.
  • Because of that, variables created within the scope of a function can only be used in that same scope
  • This is known as local scope

14 of 16

Scope

  • Variables can also be created globally and we’ve actually been creating global variables the entire time!
  • Global variables are any variables created within the main body of a python file

15 of 16

Scope

  • Generally, creating global variables that can be used within functions is not recommended.
  • In larger programs, it can make code unpredictable and harder to test.
  • However there are cases where global variables are used, such as:
    • Creating constants (data that cannot be changed throughout the program)
    • Creating configurations that exist throughout the program
    • Sharing data across multiple aspects of the program
  • Still, there are better ways to achieve these goals without using global variables, so again, not recommended!

16 of 16

See you tomorrow! And continue practicing!