Skip to main content

Lesson 4.1: Define and Call a Function

A function is a named part of a program that performs a specific job. A function definition contains the commands for that job. A function call tells the program to run those commands.

Defining a function does not run its body. The body runs each time the function is called. After the function finishes, execution returns to the command after the call.

A useful function name states the job, such as printStatus or moveRow. Clear names make the main program easier to read and make repeated work easier to change in one place.

Functions do not remove the need to plan order. A call runs exactly where it appears in the sequence.

Guided practice

Define a function named showReady that prints ready. Connect two calls to showReady under the start block. Predict the output, then run it.

Change the function body to print system ready. Both calls should now use the changed definition. Use Step to follow execution from the call into the function body and back to the main stack.

In Java, a function that belongs to a class is usually called a method. You will use the same define-and-call idea in the Java curriculum.

Loading the block workspace...

Ready to move on?

Only mark complete if you genuinely understand the material. Your progress will be saved in this browser.

Stuck on this lesson?

Ask about anything on this page. It can see which lesson you have open and which part you are reading.