1 of 20

ME 4990: Intro to CS�Object-Oriented Programming & Machine Learning 101

Functions

2 of 20

Outline

  • Function
    • Basics
    • Practice

3 of 20

Function

4 of 20

Function

  •  

5 of 20

Function

  • Equation of ellipse, is it a function for x(input)->y(output)?

6 of 20

Function

  • A function is a package
  • It is the basic element of modular design
  • For each specific input, we can have only one output
  • Which one is a function below?

7 of 20

Function in Python

  • Infinite argument, infinite return

8 of 20

Function

  • Each argument can have a name
  • When you call a function, you don’t� need to type in argument in order

9 of 20

Function

  • Argument type doesn’t matter, as long as they can be executed
  • Strongly not recommend to use this property though

10 of 20

Function

  • Function arguments can have a default value
  • Arguments with default value must be put at last

This is fine, arg1=“I say”

This is NOT fine, arguments with default value must be put at last

Best practice! Always specify argument name

Over-ride a default value

11 of 20

Function

  • You turn!
  • Test the argument settings of functions in python, do you find something new?

12 of 20

Function

  • Argument can be function!

13 of 20

Outline

  • Function
    • Basics
    • Practice

14 of 20

Function

  • Practice
  • Write a Python function reverse(string_input) to reverse a string
  • Sample input = “abcdefg”
  • Sample output = “gfedcba”
  • Cannot use python-default string reverse function

15 of 20

Hand-crafted functions

16 of 20

Hand-crafted functions

  • Calculate the surface area of a cylinder, the argument should include radius and height of a cylinder
  • You need to use 3 functions:
    • Area of the bases
    • Area of the side
    • Total area (called area) by merging the two, the function user should only use this function
  • Enhancer:
    • Set an argument called type in total area function with an additional argument called type
    • Type can be ‘cone’ and ‘cylinder’, default cylinder
    • Write additional code to calculate the area of a cone

17 of 20

Programming with AI == Assemble Functions?

  • Functions are the cells of program
  • When you have a task, you shall think from top-down approach
    • Any repetitive tasks that can be wrapped into functions?
    • How can I structure the functions to achieve my goal?

18 of 20

Program == a structure of functions

  • Function is the cell of a program
  • The Very Hungary caterpillar: https://youtu.be/75NQK-Sm1YY?si=GBcCGAZKQA8EXjIK
  • Transcript here

  • How can we make a storyteller that generate the script with whatever fruits we have?
    • For example, a mom wants to have the script: On Wednesday he eats three pineapples

19 of 20

My thoughts

  • We have some sentences are unique
  • We have some sentences are repetitive

20 of 20

Your Turn:

  • Task: build a calorie tracking system to track how much weight do I gain each day
  • What factors shall I consider for this task?
  • What functions that reflect my understanding of these factors
    • Something about eat?
      • What food?
      • The method of cooking? (Fried, boiled, etc.)
      • Time of eating?
    • Something about drink?
    • Something about exercise?
    • Anything else?
  • Discuss the system with your neighbor, what factors to consider
  • What functions do we need to reflect these factors
  • How to structure the functions
  • Ask ChatGPT to write the functions