Inputs
After this: Give a function inputs, and call it with different values.
On this page · 7 sections · 3 checks
The bakery's header function prints the same thing every time, and that is right for a header. A greeting is different. "Hello, Ana" is for Ana and "Hello, Bo" is for Bo. The lines are the same, but one word changes. To write a greeting function once, the function needs to be told who it is greeting.
In the last lesson a function was a block with a name. In this lesson the block gets inputs.
A box that is filled at the call
Put a name inside the round brackets of the first line: greet(name). That makes name a box inside the function, but an empty one. Nothing goes in it until the function is called. The call fills it: greet("Ana") runs the block with "Ana" in the box, and greet("Bo") runs it with "Bo".
The name inside the brackets of the first line is a . The value you write inside the brackets of a call is an . The argument goes into the parameter's box, and then the block runs.
Work through the two calls:
- The first line names the function
greetand gives it one parameter,name. greet("Ana")is a call with the argument"Ana". A frame appears with the boxnameholdingAna. The block printsHello, Anaand returns.greet("Bo")is a second call. A new frame appears, and its boxnameholdsBo. The block printsHello, Boand returns.
Each call gets its own frame and its own box, so the second call never sees Ana.
Watch it happen
- Run the programRun it. Watch the box name inside the frame: it holds Ana on the first call and Bo on the second.
def greet(name): print("Hello, " + name)
greet("Ana")greet("Bo")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
Two inputs, in order
A function can have several parameters, separated by commas. The arguments are matched to them in order: the first argument fills the first parameter, and the second fills the second. receipt("Bread", 2) puts "Bread" in item and 2 in count. Swap the two arguments and they land in the wrong boxes.
def receipt(item, count): print(item + " x " + str(count))
receipt("Bread", 2)receipt("Milk", 1)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The code
def greet(name): print("Hello, " + name)
greet("Ana")greet("Bo")Hello, Ana Hello, Bo
public class Main { static void greet(String name) { System.out.println("Hello, " + name); }
public static void main(String[] args) { greet("Ana"); greet("Bo"); }}Hello, Ana Hello, Bo
Run it
Add a third call with your own name, and run it again.
Check yourself
The function starts def greet(name):static void greet(String name) {, and the program calls greet("Bo"). In that call, what is the argument?
What does this print?
def show(a, b): print(a - b)
show(10, 3)show(3, 10)public class Main { static void show(int a, int b) { System.out.println(a - b); }
public static void main(String[] args) { show(10, 3); show(3, 10); }}Complete the last line: call receipt so that it prints Milk x 1.
def receipt(item, count): print(item + " x " + str(count))
receipt("Bread", 2)____________________ # ← one Milkpublic class Main { static void receipt(String item, int count) { System.out.println(item + " x " + count); }
public static void main(String[] args) { receipt("Bread", 2); ____________________ // ← one Milk }}Honesty notes
- Tell a parameter (the name in the function's first line) from an argument (the value given in the call).
- Say that each call fills the parameters afresh, in the order the arguments are written.
- Call the same function with different arguments and predict what each call prints.
- Write a Java parameter with its type, as for any other box.