68
Methods (Functions) CSE 1310 Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1

Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Methods (Functions)

CSE 1310 – Introduction to Computers and Programming

Vassilis Athitsos

University of Texas at Arlington

1

Page 2: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

2

Page 3: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

3

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);

}

}

Page 4: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

What Is a Function

• A function is a piece of code that does a specific job.

• Usually (but not always) the job is to compute something.

• Examples: – 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. 4

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);

}

}

Page 5: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

How to Write a Function

• Specify a name, like square.

5

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

Page 6: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

How to Write a Function

• Specify a name, like square.

• Specify the inputs (called arguments). – The arguments are the

input that the function needs in order to compute an output.

– For example, what does the square function take as input?

6

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);

}

}

Page 7: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

How to Write a Function

• Specify a name, like square.

• Specify the inputs (called arguments). – The arguments 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. 7

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)

Page 8: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

How to Write a Function

• Specify a name, like square.

• Specify the inputs (called arguments).

• 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?

8

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

Page 9: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

How to Write a Function

• Specify a name, like square.

• Specify the inputs (called arguments).

• Specify the return type of the function.

• Specify what the function does. This is what we call the body of the function.

9

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

Page 10: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

How to Write a Function

• Specify a name, like square.

• Specify the inputs (called arguments).

• 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. 10

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

Page 11: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Using a Function (Function Calls)

• Once you have written a function, you can use it anywhere in your code, by doing a function call.

11

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

Page 12: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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).

12

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)

Page 13: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

13

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

Page 14: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

14

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

Page 15: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

15

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

Page 16: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

16

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

Page 17: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

• You can use it as an argument for another function call.

17

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);

}

}

the return value is used as argument for another function call

Page 18: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Function Calls

• You can call a function as many times as you like.

• On the next slide, we call the square function three times.

18

Page 19: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

19

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);

}

}

Example: Calling the square function three times.

Page 20: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

20

Page 21: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

– Put placeholders where more detail is needed.

21

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);

}

}

}

}

Page 22: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

22

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);

}

}

}

}

Page 23: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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?

23

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);

}

}

}

}

Page 24: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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

24

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);

}

}

}

}

Page 25: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

25

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);

}

}

}

}

Page 26: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

26

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.

Page 27: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

27

Output:

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);

}}}}

Page 28: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Why Do We Need Functions

• To write better code:

– More correct, easier to read/write/change.

• To write complicated code.

• Functions help us organize code.

YOU CANNOT WRITE NON-TRIVIAL PROGRAMS IF YOU DO NOT USE FUNCTIONS

28

Page 29: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

29

Page 30: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

• First step: – Write the basic

structure of the program.

– Put placeholders where more detail is needed.

30

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);

}

}

Page 31: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

• Second step: – Identify functions that

can be used to complete the program.

• We can use the is_prime function again!!!

31

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);

}

}

Page 32: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

• 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!!!

32

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);

}

}

Page 33: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

33

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: 1000000

1000003

Example Output:

Enter an integer: 123456789

123456791

Example Output:

Enter an integer: 7500

7507

Page 34: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Making Code Easier to Read

• In lots of programs, 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.

34

Page 35: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Example: Input Validation

• Write a program that:

– Asks the user to enter integers A, B, C. • if the user enters an invalid input, the program should keep asking

the user to enter a valid integer,

– Prints out the sum of the three integers.

35

Page 36: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Example: Input Validation

• Write a program that:

– Asks the user to enter integers A, B, C. • if the user enters an invalid input, the program should keep asking

the user to enter a valid integer,

– Prints out the sum of the three integers.

• A first version of the solution can be seen on the next slide.

36

Page 37: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

import java.util.Scanner;

public class example1 {

public static void main(String[] args)

{

Scanner in = new Scanner(System.in);

int A;

while (true)

{

System.out.printf("Enter integer A: ");

String s = in.next();

try

{

A = Integer.parseInt(s);

}

catch (Exception e)

{

System.out.printf("Invalid input\n");

continue;

}

break;

}

int B;

while (true)

{

System.out.printf("Enter integer B: ");

String s = in.next();

try

{

B = Integer.parseInt(s);

}

catch (Exception e)

{

System.out.printf("Invalid input\n");

continue;

}

break;

}

int C;

while (true)

{

System.out.printf("Enter integer C: ");

String s = in.next();

try

{

C = Integer.parseInt(s);

}

catch (Exception e)

{

System.out.printf("Invalid input\n");

continue;

}

break;

}

System.out.printf("A + B + C = %d\n", A +

B + C);

}

}

Page 38: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Example: Input Validation

• Write a program that:

– Asks the user to enter integers A, B, C. • if the user enters an invalid input, the program should keep asking

the user to enter a valid integer,

– Prints out the sum of the three integers.

• A first version of the solution can be seen on the previous slide.

• What is wrong with that code?

38

Page 39: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Example: Input Validation

• Write a program that:

– Asks the user to enter integers A, B, C. • if the user enters an invalid input, the program should keep asking

the user to enter a valid integer,

– Prints out the sum of the three integers.

• A first version of the solution can be seen on the previous slide.

• What is wrong with that code?

– It is too long.

– It is hard to read and understand.

39

Page 40: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Example: Input Validation

• Solution:

40

Page 41: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Example: Input Validation

• Solution: write a function for getting an integer from the user.

• Name: user_integer

• Arguments:

• Return value:

41

Page 42: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Example: Input Validation

• Solution: write a function for getting an integer from the user.

• Name: user_integer

• Arguments:

– One argument - what we print to the user.

– Type of the argument: String

• Return value:

– int

42

Page 43: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

import java.util.Scanner;

public class example1

{

public static int user_integer(String message)

{

Scanner in = new Scanner(System.in);

int result;

while (true)

{

System.out.printf(message);

String s = in.next();

try

{

result = Integer.parseInt(s);

}

catch (Exception e)

{

System.out.printf("%s is not a valid integer.\n\n", s);

continue;

}

return result;

}

}

public static void main(String[] args) {

int A = user_integer("Enter integer A: ");

int B = user_integer("Enter integer B: ");

int C = user_integer("Enter integer C: ");

System.out.printf("A + B + C = %d\n", A + B + C);

}}

• Here we see a solution that defines and uses the user_integer function.

• The code is shorter.

• Unlike the previous version, code is not duplicated.

• The main function is short and easy to read.

Page 44: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

44

Enter string S: hello

Enter integer N: 5

hello

hello

hello

hello

hello

Example Output:

Page 45: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

45

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.

}

}

Page 46: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

46

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);

}

}

Page 47: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

47

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);

}

}

Page 48: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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. 48

Page 49: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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?

49

Page 50: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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)

50

Page 51: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

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.

51

Page 52: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

What Will This Program Do?

52

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);

System.out.printf("var2 = %s\n", var2);

}

}

Page 53: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

53

Current line

variables in main:

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);

System.out.printf("var2 = %s\n", var2);

}

}

Page 54: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

54

First line to execute:

variables in main:

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);

System.out.printf("var2 = %s\n", var2);

}

}

Page 55: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

55

variables in main: var1 = "hello"

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);

System.out.printf("var2 = %s\n", var2);

}

}

First line creates variable var1

Page 56: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

56

Next line to execute:

variables in main: var1 = "hello"

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);

System.out.printf("var2 = %s\n", var2);

}

}

Page 57: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

57

Next line to execute:

variables in main: var1 = "hello" var2 = "goodbye"

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);

System.out.printf("var2 = %s\n", var2);

}

}

Page 58: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

58

Next line to execute:

variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth"

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);

System.out.printf("var2 = %s\n", var2);

}

}

Page 59: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

59

Next line to execute: function call

variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon"

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);

System.out.printf("var2 = %s\n", var2);

}

}

Page 60: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

60

Next line to execute: function call. First, assign values to arguments.

variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon"

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);

System.out.printf("var2 = %s\n", var2);

}

}

variables in foo: var1 = ??? var2 = ???

Page 61: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

61

Next line to execute: function call. First, assign values to arguments.

variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon"

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);

System.out.printf("var2 = %s\n", var2);

}

}

variables in foo: var1 = "earth" var2 = "moon"

Page 62: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

62

Next line to execute: How does Java know which var1 to print?

variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon"

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);

System.out.printf("var2 = %s\n", var2);

}

}

variables in foo: var1 = "earth" var2 = "moon"

Page 63: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

63

Next line to execute:

variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon"

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);

System.out.printf("var2 = %s\n", var2);

}

}

variables in foo: var1 = "earth" var2 = "moon"

Currently executing the body of foo. Only the variables of foo are visible. “earth" is printed.

Page 64: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

64

Next line to execute:

variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon"

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);

System.out.printf("var2 = %s\n", var2);

}

}

variables in foo: var1 = "earth var2 = "moon"

"moon" is printed.

Page 65: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

65

Next line to execute:

variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon"

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);

System.out.printf("var2 = %s\n", var2);

}

}

variables in foo: var1 = "earth" var2 = "moon"

Which line comes next?

Page 66: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

66

Next line to execute:

variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon"

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);

System.out.printf("var2 = %s\n", var2);

}

}

Done with the function call. The variables of foo disappear.

Page 67: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Step-by-step Execution

67

Next line to execute:

variables in main: var1 = "hello" var2 = "goodbye" var3 = "earth" var4 = "moon"

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);

System.out.printf("var2 = %s\n", var2);

}

}

"goodbye" is printed.

Page 68: Methods (Functions)vlm1.uta.edu/~athitsos/courses/cse1310_fall2016/... · Terminology: Methods/Functions •In Java, the term methods is the one mainly used. •In some other languages,

Summary of Program Output

68

Output: var1 = earth var2 = moon var2 = goodbye

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);

System.out.printf("var2 = %s\n", var2);

}

}