A girl and a robot thinking together

Think it through together · A little story

The robot has to say hello three times every day. The girl does not want to write the same actions in many places, so she gives this group of actions a name.

Lay out the process

Follow the numbered steps, then explain them in your own words
  1. Package the actionsDefine greet
  2. When neededCall greet
  3. After it runsReturn to where you were

Functions give a task a clear name and boundary so a program can reuse that logic.

A definition is like writing a task card; calling the function is what runs the task. Choose a name that gives readers a clue about its job.

01What if you need to use a piece of code many times?

Imagine that a program needs to find the larger of two numbers in many places. If we rewrite the same comparison each time, the program gets longer and it becomes easier to miss something when making a change.

A function can give this task its own name, so we can call it whenever we need it.

02What exactly is a function?

A function is a named piece of program code that can be called to carry out a particular task.

int maxValue(int a, int b) {
    if (a > b) {
        return a;
    }
    return b;
}

This function receives two integers and returns the larger one.

Name

maxValue tells people what it does.

Input

The parameters a and b.

Output

An int return value.

03Can functions reduce repeated code?

int x = maxValue(3, 8);
int y = maxValue(20, 5);

The same function code can be called many times, using different data each time.

04Why can functions make a program easier to read?

All the code crowded into main

Readers must understand many details at once.

Split into several functions

The function names first tell readers what each part does.

For example, readInput(), calculate(), and printResult() are easier to understand than one long stretch of code.

05Are more functions always better?

No. Functions should be divided around clear, reasonably complete tasks.

If every line of code becomes its own function, the program may jump around so much that it is harder to read.

06How do functions help us solve big problems?

Big problem→Break it into smaller tasks→Let each function handle a part

This is an important idea in program design: break a problem down, then combine the results.

What you have learned

  • A function is a piece of code that can be called to perform a particular task.
  • A function can have a name, parameters, and a return value.
  • Functions can reduce repetition and make program structure clearer.
  • More functions are not always better; divide code around clear responsibilities.
  • Functions are an important tool for breaking big problems into smaller ones.

Next: How do you define and call a function?

Your turn to try

If you write a greeting function but never call it, will the program greet anyone automatically?

Have you thought it through? Open for an explanation

No. A definition tells the program which task is available; a call triggers that task for this run.