Task 1 of 7 · 0 solved · 7 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

3.2 Methods — a method with a parameter

Lesson

Passing values into a method

A parameter is a named input listed in a method's (). It lets one method work on different values: you supply a value when you call it, and the method uses it. That makes a method flexible instead of doing the exact same thing every time.


Worked example

void greet(String name) {
    System.out.println("Hello, " + name + "!");
}

greet("Ada");   // Hello, Ada!
greet("Sam");   // Hello, Sam!

The parameter name takes a different value on each call.


▶ Try it: greet(String name) is defined for you. Run this, then change the names in the calls (or add more) and re-run.



greet(String name) prints Hello, then the name then !. What does greet("Sam") print?


In the call greet("Sam"), what is "Sam"?


Fill the blank to call greet with the name Ada.


A method hi(String name) prints Hi then the name. Type exactly what hi("Bo") prints:


True or false?