Methods (Functions) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington.

Slides:



Advertisements
Similar presentations
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Advertisements

Chapter 7: User-Defined Methods
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 4: Control Structures II
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
The while-statement. Syntax and meaning of the while-statement The LOOP-CONTINUATION-CONDITION is a Boolean expression (exactly the same as in the condition.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos and Alexandra Stefan University of Texas at Arlington 1.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Decisions (If Statements) And Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Strings CSE 1310 – Introduction to Computers and Programming
Sophomore Scholars Java
Strings CSE 1310 – Introduction to Computers and Programming
CSC111 Quick Revision.
Strings CSE 1310 – Introduction to Computers and Programming
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Boolean Expressions and Conditionals (If Statements)
Formatted Output (printf)
Exceptions and User Input Validation
Introduction to Computer Science / Procedural – 67130
Primitive Data, Variables, Loops (Maybe)
TK1114 Computer Programming
First Programs CSE 1310 – Introduction to Computers and Programming
Common Mistakes with Functions
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Building Java Programs
An Introduction to Java – Part I, language basics
Java Programming Loops
T. Jumana Abu Shmais – AOU - Riyadh
CS18000: Problem Solving and Object-Oriented Programming
Introduction to Computer Programming
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Array Lists CSE 1310 – Introduction to Computers and Programming
Chapter 6: User-Defined Functions I
Building Java Programs
Chapter 2 Programming Basics.
CIS 110: Introduction to Computer Programming
Java Programming Loops
Suggested self-checks: Section 7.11 #1-11
Decisions (If Statements) And Boolean Expressions
Strings CSE 1310 – Introduction to Computers and Programming
Suggested self-checks:
First Programs CSE 1310 – Introduction to Computers and Programming
ITM 352 Functions.
Input and Formatted Output (printf)
Computer Science Club 1st November 2019.
Presentation transcript:

Methods (Functions) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington

What Is a Method? A method is a mechanism for allowing a piece of code to be executed more than once, in a safe way. Examples of method usage: s.length(), s.indexOf(s2), Math.floor(3.4)… Syntax Communication with the calling line. Signature of the method Parameters Return value (when appropriate)

What Is a Method A method is a piece of code that does a specific job. Usually the job is to compute and return something. Examples: Seen: s.length(), s.indexOf(s2), s.charAt(i), Math.floor(n), Computing the square of a number. Determining if an number is prime. Computing the number of digits of a number. Counting the number of vowels in a string. Some functions will not return anything. Examples: Seen: System.out.printf(….), Print: a banner, a table, a menu Change/interact with the file system Write in a file.

Terminology: Methods/Functions In Java, the term methods is the one mainly used. In some other languages, the term functions is more common. In some languages (e.g., C++) methods and functions have minor differences in meaning. In this course, I will use the two terms (methods and functions) as synonyms. New terms: Function/method Function call, Caller function (caller) vs function being called (callee) Arguments/parameters Return, return value, ‘what does the function return?’ Method signature

Example import java.util.Scanner; public class example1 { public static double square(double number) { double result = number * number; return result; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square);

A First Example Here we see our first example of a function. It is called square. Its job is to compute the square of a number. import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square);

How to Write a Function Specify a name, like square. function name import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square); function name

How to Write a Function Specify a name, like square. Specify the inputs (called parameters). The parameters are the input that the function needs in order to compute an output. For example, what does the square function take as input? import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square);

How to Write a Function Specify a name, like square. Specify the inputs (called parameters). The parameters are the input that the function needs in order to compute an output. For example, what does the square function take as input? A number. We can have functions with 0, 1, or more arguments. We will see examples later. import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square); parameter(s)

How to Write a Function Specify a name, like square. Specify the inputs (called parameters). Specify the return type of the function. What is the type of the value that the function computes? In other words, what values are legal and illegal for the output of the function? import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square); return type

How to Write a Function Specify a name, like square. Specify the inputs (called parameters). Specify the return type of the function. Specify what the function does. This is what we call the body of the function. import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square); body

How to Write a Function Specify a name, like square. Specify the inputs (called parameters). Specify the return type of the function. Specify what the function does. This is what we call the body of the function. Specify what value the function returns, using one or more return statements. import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square); return statement

Using a Function (Function Calls) Once you have written a function, you can use it anywhere in your code, by doing a function call. import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square); function call

Using a Function (Function Calls) Once you have written a function, you can use it anywhere in your code, by doing a function call. When you call a function, you must: Provide the appropriate argument(s). import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square); argument(s)

Using a Function (Function Calls) Once you have written a function, you can use it anywhere in your code, by doing a function call. When you call a function, you must: Provide the appropriate argument(s). Use the return value appropriately. import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square); storing the return value

Using the Return Value In the square function, the return value is a double. You can use this value in any way that you can use any double number. Examples: You can store the return value in a variable. import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square); storing the return value in a variable

Using the Return Value In the square function, the return value is a double. You can use this value in any way that you can use any double number. Examples: You can store the return value in a variable. You can print the return value directly. import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); System.out.printf("%.2f squared = %.2f\n", N, square(N)); printing the return value directly

Using the Return Value In the square function, the return value is a double. You can use this value in any way that you can use any double number. Examples: You can store the return value in a variable. You can print the return value directly. You can use it as part of a more complicated expression. import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double my_var = 18 + square(N); System.out.printf("result = %.2f\n", my_var); using the return value in an expression

Using the Return Value import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number: "); double N = in.nextDouble(); double my_var = 18 + square(square(N)); System.out.printf("result = %.2f\n", my_var); In the square function, the return value is a double. You can use this value in any way that you can use any double number. Examples: You can store the return value in a variable. You can print the return value directly. You can use it as part of a more complicated expression. You can use it as an argument for another function call. the return value is used as argument for another function call

Function Calls You can call a function as many times as you like. On the next slide, we call the square function three times.

Example: Calling the square function many times. import java.util.Scanner; public class example1 { public static double square(double number) double result = number * number; return result; } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Please enter a number M: "); double M = in.nextDouble(); System.out.printf("Please enter a number N: "); double N = in.nextDouble(); double M_square = square(M); System.out.printf("%.2f squared = %.2f\n", M, M_square); double N_square = square(N); System.out.printf("%.2f squared = %.2f\n", N, N_square); double MN_square = square(M*N); System.out.printf("%.2f squared = %.2f\n", M*N, M*N_square); System.out.printf("%.2f squared = %.2f\n", N, square(N) ); System.out.printf("%.2f to the 4th= %.2f\n", N, square(square(N)) ); Example: Calling the square function many times.

Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. Identify the solution components ………………….

Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. Identify the solution components Read N from user. Generate ints 2 to N. Print numbers that satisfy a certain property. Try with an easier property: even Check if a single int is prime.

Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. First step: Write the basic structure of the program. (steps 1-3) Put placeholders where more detail is needed. import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextInt(); for (int i = 2; i <= N; i++) { if i is prime System.out.printf("%d\n", i); }

Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. Second step: Identify functions that can be used to complete the program. import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextInt(); for (int i = 2; i <= N; i++) { if i is prime System.out.printf("%d\n", i); }

Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. We can use a function is_prime to check if i is prime. Arguments? Return type? import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextInt(); for (int i = 2; i <= N; i++) { if i is prime System.out.printf("%d\n", i); }

Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. We can use a function is_prime to check if i is prime. Arguments? an integer Return type? boolean import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextInt(); for (int i = 2; i <= N; i++) { if i is prime System.out.printf("%d\n", i); }

Example: Printing Prime Numbers Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. Step 3: use the function. We have not written the function yet, but that is OK. Obviously, the program will not run until we write the function. import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextInt(); for (int i = 2; i <= N; i++) { if (is_prime(i)) System.out.printf("%d\n", i); }

Step 4: write the function. import java.util.Scanner; public class example1 { public static boolean is_prime(int N) { for (int i = 2; i < N; i++) if (N % i == 0) return false; } return true; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextInt(); for (int i = 2; i <= N; i++) if (is_prime(i)) System.out.printf("%d\n", i); }}}} Write a program that: Asks the user to enter an integer N. Prints all prime integers from 2 up to (and including) N. Step 4: write the function.

Output: import java.util.Scanner; public class example1 { Enter an integer: 80 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 import java.util.Scanner; public class example1 { public static boolean is_prime(int N) { for (int i = 2; i < N; i++) if (N % i == 0) return false; } return true; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextInt(); for (int i = 2; i <= N; i++) if (is_prime(i)) System.out.printf("%d\n", i); }}}}

Why Do We Need Functions To write better code: More correct, easier to read/write/change. To write complicated code. Like we did in the example above: we could focus on different aspects without entangling the code : generating the numbers, checking if prime or not. Functions ‘reduce’ the complexity of your code. E.g. using a function, a nested loop could become a single loop (and the code for the inner loop will be in the function). Functions help us organize code and share code. YOU CANNOT WRITE NON-TRIVIAL PROGRAMS IF YOU DO NOT USE FUNCTIONS

Example: Prime Numbers, Again Write a program that: Asks the user to enter an integer N. Prints out the smallest prime number greater than or equal to N. Identify the solution components ………………….

Example: Prime Numbers, Variation Write a program that: Asks the user to enter an integer N. Prints out the smallest prime number greater than or equal to N. Identify the solution components Read N. Generate ints from N up. Stop when an int has a property (e.g. even). ??placeholder Check if an int is prime.

Example: Prime Numbers, Variation Write a program that: Asks the user to enter an integer N. Prints out the smallest prime number greater than or equal to N. First step: Write the basic structure of the program. Put placeholders where more detail is needed. import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextInt(); int i = N; while (i is not prime) { i++; } System.out.printf("%d\n", i);

Example: Prime Numbers, Variation Write a program that: Asks the user to enter an integer N. Prints out the smallest prime number greater than or equal to N. Second step: Identify functions that can be used to complete the program. We can use the is_prime function again!!! import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextInt(); int i = N; while (is_prime(i) == false) { i++; } System.out.printf("%d\n", i);

Functions make it really easy to re-use code!!! import java.util.Scanner; public class example1 { public static boolean is_prime(int N) for (int i = 2; i < N; i++) if (N % i == 0) return false; } return true; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextInt(); while (is_prime(N) == false) N++; System.out.printf("%d\n", N); Write a program that: Asks the user to enter an integer N. Prints out the smallest prime number greater than or equal to N. To complete the program, we can just use the is_prime function we already have. Functions make it really easy to re-use code!!!

Example Output: Example Output: Example Output: Enter an integer: 1000000 1000003 import java.util.Scanner; public class example1 { public static boolean is_prime(int N) for (int i = 2; i < N; i++) if (N % i == 0) return false; } return true; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter an integer: "); int N = in.nextInt(); while (is_prime(N) == false) N++; System.out.printf("%d\n", N); Example Output: Enter an integer: 123456789 123456791 Example Output: Enter an integer: 7500 7507

Making Code Easier to Read Often times, we need to perform a task many times. Copying and pasting code can work, but has disadvantages: The resulting code can look long and ugly. If we make a mistake, we end up copying and pasting the mistake many times. Fixing such mistakes, that have been copied and pasted many times, can be a pain. Write a function instead

Example: Repeated Printing Write a program that: Asks the user to enter a string S. Asks the user to enter a number N. Prints N times string S. Enter string S: hello Enter integer N: 5 hello Example Output:

Example: Repeated Printing Write a program that: Asks the user to enter a string S. Asks the user to enter a number N. Prints N times string S. Let's write a function, that: takes as input a string S and an integer N. prints the string N times. import java.util.Scanner; public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter string S: "); String S = in.next(); System.out.printf("Enter integer N: "); int N = in.nextInt(); //print String S repeatedly, N times. }

Example: Repeated Printing The complete program is shown on the right. Function repeat_print has two features we have not seen before: It takes two arguments. It returns nothing. import java.util.Scanner; public class example1 { public static void repeat_print(String message, int times) for (int i = 0; i < times; i++) System.out.printf("%s\n", message); } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Enter string S: "); String S = in.next(); System.out.printf("Enter integer N: "); int N = in.nextInt(); repeat_print(S, N);

A Function That Returns Nothing Function repeat_print does something useful. However, we do not want any value returned from the function. In that case, we specify the return type of the function as void. import java.util.Scanner; public class example1 { public static void repeat_print(String message, int times) for (int i = 0; i < times; i++) System.out.printf("%s\n", message); } public static void main(String[] args) Scanner in = new Scanner(System.in); System.out.printf("Enter string S: "); String S = in.next(); System.out.printf("Enter integer N: "); int N = in.nextInt(); repeat_print(S, N);

The main Function When Java executes a program, how does Java know where to start? Every program must have a function called main, such that: It takes one argument, of type String []. We will understand this type in our next topic, when we do arrays. the return type is void. Until we saw how to write functions, all our code used to go to main. Now that we have started using functions, the main code will be a relatively small part of the program. Functions will do the bulk of the work.

Function Arguments Functions have arguments. To call a function XYZ, you write something like : XYZ(argument_1, …, argument_N) How would you know how many arguments to use?

Function Arguments Functions have arguments. To call a function XYZ, you write something like: XYZ(argument_1, …, argument_N) How would you know how many arguments to use? From the function definition, which (among other things) defines EXACTLY: how many arguments to use. the type of each argument. the order of the arguments. public static type XYZ(type_1 arg_1, …, type_N arg_N)

Executing a Function Call When the body of the function starts executing, the only variables visible to the function are: the arguments. variables defined in the body of the function.

What Will This Program Do? public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4);

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: Current line

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: First line to execute:

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" First line creates variable var1

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" Next line to execute:

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" Next line to execute:

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" Next line to execute:

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" Next line to execute: function call

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: var1 = ??? var2 = ??? Next line to execute: function call. First, assign values to arguments.

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: var1 = "earth" var2 = "moon" Next line to execute: function call. First, assign values to arguments.

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: var1 = "earth" var2 = "moon" Next line to execute: How does Java know which var1 to print?

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: var1 = "earth" var2 = "moon" Next line to execute: Currently executing the body of foo. Only the variables of foo are visible. “earth" is printed.

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: var1 = "earth var2 = "moon" Next line to execute: "moon" is printed.

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" variables in foo: var1 = "earth" var2 = "moon" Next line to execute: Which line comes next?

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" Next line to execute: Done with the function call. The variables of foo disappear.

Step-by-step Execution public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon" Next line to execute: "goodbye" is printed.

Summary of Program Output public class example1 { public static void foo(String var1, String var2) System.out.printf("var1 = %s\n", var1); System.out.printf("var2 = %s\n", var2); } public static void main(String[] args) String var1 = "hello"; String var2 = "goodbye"; String var3 = "earth"; String var4 = "moon"; foo(var3, var4); Output: var1 = earth var2 = moon var2 = goodbye

Practice Write functions Based on problem description: “Write a function that takes [as argument] a string S and returns the sum of the ASCII code of the characters in it. Write a function that takes 2 strings, a piece size, p_sz, and merges the strings in chunks of p_sz. “Write a function that takes [as argument] a list L and returns the sum of the elements in L. If L has strings, it should return the concatenation of all the strings.” Write a fct that takes two arguments: a String, s, and a char ,c, and removes all the occurrences of c from the string s. Test cases: s = ‘aaxyaj’, c = ‘a’ // s = “aaaaaaaa”, c = ‘a’ // s = “aaa”, c = ‘a’ return a new string with the modifications . Based on your own choice.