5/18/20 1
INTRO TO OOP FOR DATA SCIENCE
- PROF. JOHN GAUCH
FUNCTIONS
OVERVIEW
OVERVIEW
§ In real life, we often find ourselves doing the same task
- ver and over
§ Example: make toast and jam for breakfast in the morning § Example: drive our car from home to work and back § Once we have decided on our favorite way to do these tasks, we can write down the steps we normally take § Make a recipe card for making breakfast items § Write down series of turns to travel from A to B § This written descriptions will let others reuse our work
(c) Prof. John Gauch, Univ. of Arkansas, 2020
3
OVERVIEW
§ Functions in Java allow us to reuse code § When we declare a function we write down the code we want to reuse in our program § We can call a function to execute this code anywhere we want in our program § We can customize a function by providing input parameters and calculate results using this input § Functions provide a fast/simple way to reuse code which saves a lot of programmer time/effort
(c) Prof. John Gauch, Univ. of Arkansas, 2020
4
OVERVIEW
§ Functions play an important role in software development § Break a big problem down into a sequence of smaller problems that we know how to solve § Implement and test solutions to each of the smaller problems using functions § Use the functions we created above to create an effective solution to the big problem § Functions in the same category are often packaged together to create function libraries § Java has created function libraries for input/output, string manipulation, mathematics, random numbers, etc.
(c) Prof. John Gauch, Univ. of Arkansas, 2020
5
OVERVIEW
§ Lesson objectives: § Learn the syntax for declaring functions in Java § Learn how to call and trace the execution of functions § Learn how to define and use function parameters § Study example programs showing their use § Complete online labs on functions § Complete programming project using functions
(c) Prof. John Gauch, Univ. of Arkansas, 2020
6