5/18/20 INTRO TO OOP FOR FUNCTIONS DATA SCIENCE PROF. JOHN GAUCH OVERVIEW OVERVIEW OVERVIEW § In real life, we often find ourselves doing the same task § Functions in Java allow us to reuse code over and over § When we declare a function we write down the code we want to reuse in our program § Example: make toast and jam for breakfast in the morning § Example: drive our car from home to work and back § We can call a function to execute this code anywhere we want in our program § Once we have decided on our favorite way to do these tasks, we can write down the steps we normally take § We can customize a function by providing input § Make a recipe card for making breakfast items parameters and calculate results using this input § Write down series of turns to travel from A to B § This written descriptions will let others reuse our work § Functions provide a fast/simple way to reuse code which saves a lot of programmer time/effort 3 4 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 OVERVIEW OVERVIEW § Functions play an important role in software development § Break a big problem down into a sequence of smaller § Lesson objectives: problems that we know how to solve § Learn the syntax for declaring functions in Java § Implement and test solutions to each of the smaller § Learn how to call and trace the execution of functions problems using functions § Learn how to define and use function parameters § Use the functions we created above to create an effective § Study example programs showing their use solution to the big problem § Complete online labs on functions § Complete programming project using functions § 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. 5 6 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 1
5/18/20 DECLARING FUNCTIONS § In order to declare a function in Java we need to provide FUNCTIONS the following information: § The name of the function § List of operations to be performed § Type of data value to be returned § Types and names of parameters (if any) § Declaration of local variables (if any) § Value to be returned to the main program (if any) PART 1 CREATING AND USING FUNCTIONS 8 (c) Prof. John Gauch, Univ. of Arkansas, 2020 DECLARING DECLARING FUNCTIONS FUNCTIONS § Function return values § Consider the following programming task: § The return( ) statement exits the function, and returns a § Prompt the user to input a value between 1..9 value back to the program where the function was called § Read the integer value from the user § You can use the return( ) statement anywhere in function, § Loop until a valid input value is entered but the bottom of the function is preferred § Prompt the user to input a value between 1..9 § Read the integer value from the user § The type of the return value depends on the application § Typical mathematical functions return float values § We can declare a Java function to package this code together so it can be reused in different programs § Typical I/O functions return input data or status flags § Some functions perform calculations but return no value (we use the special data type "void" in this case) 9 10 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 DECLARING DECLARING FUNCTIONS FUNCTIONS // Function declaration example // Function declaration example Name of the static int ReadNum() static int ReadNum() function we are declaring { { int Number = 0; int Number = 0; while ((Number < 1) || (Number > 9)) while ((Number < 1) || (Number > 9)) { { Operations to System.out.print("Enter a number between 1..9: "); System.out.print("Enter a number between 1..9: "); be performed by the function Number = scanner.nextInt(); Number = scanner.nextInt(); } } return( Number ); return( Number ); } } 11 12 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 2
5/18/20 DECLARING DECLARING FUNCTIONS FUNCTIONS // Function declaration example // Function declaration example This local variable static int ReadNum() Data type of static int ReadNum() can only be used the function { { within this function return value int Number = 0; int Number = 0; while ((Number < 1) || (Number > 9)) while ((Number < 1) || (Number > 9)) { { System.out.print("Enter a number between 1..9: "); System.out.print("Enter a number between 1..9: "); Number = scanner.nextInt(); Number = scanner.nextInt(); } } return( Number ); return( Number ); } } 13 14 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 DECLARING FUNCTIONS CALLING FUNCTIONS // Function declaration example § Now that we have declared the function ReadNum how can we use this function in the main program? static int ReadNum() { § We need to specify the following in a function call int Number = 0; § The name of the function we wish to execute while ((Number < 1) || (Number > 9)) § Variables passed in as parameters (if any) { § Operations to be performed with return value (if any) System.out.print("Enter a number between 1..9: "); § The main program will jump to function code, execute it, Number = scanner.nextInt(); and return with the value that was calculated } Value returned by § Return values can be used to assign variables or they can return( Number ); the function to the be output (we should not ignore them) main program } 15 16 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 CALLING FUNCTIONS CALLING FUNCTIONS public static void main (String[] args) § We can trace the executions functions in a program using the "black box" method { This function call will cause the main program § Draw a box for the main program … to jump to the ReadNum § Draw a second box for the function being called function, execute that // Function usage example § Draw an arrow from the function call in the main program code and store the return int Num = 0; value in variable Num to the top of the function being called § Draw a second arrow from the bottom of the function back Num = ReadNum(); to the main program labeled with the return value cout << "Your Lucky number is " << Num << endl; § Using this black box diagram we can visualize the … execution of the program by following the arrows } 17 18 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 3
5/18/20 CALLING FUNCTIONS CALLING FUNCTIONS public static int main() static int ReadNum() public static int main() static int ReadNum() { { { { return(…); return(…); Num = ReadNum(); } Num = ReadNum(); } We leave the main We then execute the code program and jump to inside the ReadNum function } } the ReadNum function 19 20 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 CALLING FUNCTIONS CALLING FUNCTIONS public static int main() { public static int main() static int ReadNum() { { … ReadNum returned the … // Function usage example value 6, which is now stored in Num variable int Num = 0; return(…); Num = ReadNum(); Num = ReadNum(); } 6 cout << "Your Lucky number is " << Num << endl; When we reach the bottom of the … … ReadNum function we return a } value to the main program. } If the user enters the number 6 the return value would be 6 21 22 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 FUNCTIONS WITHOUT RETURN VALUES CALLING FUNCTIONS public static int main() § What happens if a function does not have a return value? { Each time we call ReadNum § In some cases, we just want a function to print out the code in the function will something like a help message or a command menu … be executed and a new // Function usage example § In this case, the function return type is void value will be returned § Since the function does not need to return anything to the int Num = 0; main program, we can omit the return statement Num = ReadNum(); § When the code reaches the bottom of the function, it will cout << "Your lucky number is << Num << endl; automatically return to the location the function was called Num = ReadNum(); in the main program cout << "Your second lucky number is " << Num << endl; … } 23 24 (c) Prof. John Gauch, Univ. of Arkansas, 2020 (c) Prof. John Gauch, Univ. of Arkansas, 2020 4
Recommend
More recommend