Module 4: Method basics Flashcards

1
Q

What is a method?

A

A block of code used to perform a task and only runs when you call it. Methods only need to be written once. A method must be declared in a class.

Ex creating:

public void method() {}

Ex calling:

method();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are method parameters?

A

Code inside their parentheses which act like variables inside the method. They can take multiple parameters, separate them by a comma. Parameters passed to a method are called “arguments”.

Ex creating:

public void default(int A, double B) {}

Ex calling:

default(11, 20.5);

Note if you use multiple parameters, when you call the method it must have the same number of arguments as there is parameters, and the arguments must be passed in the same order.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a return type?

A

When you want a method to return a value, use a primitive data type before the method name, then use a return keyword inside.

Ex:

static double percent(double num, int percentage) {double res = num*percentage/100; return res;}

It is good practice to store a returned result from a method in a variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does the void keyword do?

A

Specifies a method should not have a return value.

Ex:

public void method() {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly