Higher-order Functions 9/30/2019 Slides adapted from Berkeley CS61a
Higher-Order Functions 9 / 30 / 2019 Slides adapted from Berkeley CS61a
Higher-Order Functions Functions are first-class,meaning they can be manipulated as values A higher-order function is: 1.A function that takes a function as an argument and/or 1.A function that returns a function as a return value
Higher-Order Functions Functions are first-class, meaning they can be manipulated as values A higher-order function is: 1. A function that takes a function as an argument and/or 1. A function that returns a function as a return value
Designing Functions
Designing Functions
Describing Functions def square(x): """Return X *x""" A function's domain is the set of all inputs it might x is a number possibly take as arguments. A function's range is the set of output values it square returns a non- might possibly return. negative real number A pure function's behavior is the relationship it square returns the square of x creates between input and output
Describing Functions A function's domain is the set of all inputs it might possibly take as arguments. A function's range is the set of output values it might possibly return. A pure function's behavior is the relationship it creates between input and output. def square(x): """Return X * X""" x is a number square returns a nonnegative real number square returns the square of x
A Guide to Designing Function Give each function exactly one job,but make it apply to many related situations >>>round(1.23) >>round(1.23,1) >>round(1.23,0) >>round(1.23,5) 1 1.2 1 1.23 Don't repeat yourself (DRY).Implement a process just once,but execute it many times
A Guide to Designing Function Give each function exactly one job, but make it apply to many related situations >>> round(1.23) 1 >>> round(1.23, 1) 1.2 >>> round(1.23, 0) 1 >>> round(1.23, 5) 1.23 Don’t repeat yourself (DRY). Implement a process just once, but execute it many times