Return Value

A return value is the result that a function sends back after it finishes executing.

It allows a function to produce an output that can be used elsewhere in the program.

Why return values are important

Without a return value, a function only performs an action.

With a return value, a function can:

  • Produce results
  • Perform calculations
  • Pass data to other parts of the program
  • Be used inside expressions

Return values make functions powerful and useful.

How return works

A function uses the return keyword to send a value back.

 

Example:

function add(a, b) { return a + b }

When the function is called:

result = add(5, 3)

The function returns 8, and result stores that value.

What happens after return

When a function reaches a return statement:

  • It immediately stops executing
  • It sends the value back
  • The function ends

Any code written after return inside the function will not run.

Functions without return

Some functions do not return a value.

 

Example:

function greet() { print("Hello") }

This function performs an action but does not return data.

Why learning return values matters

Return values help you:

  • Chain functions together
  • Store results in variables
  • Build complex calculations
  • Write clean and modular code

Most real programs depend heavily on returned values.

Simple example

Think of a vending machine:  You insert money and select an item. The machine processes the input and returns a product.  That product is the return value.

Related terms

Source

Simplified from general programming documentation and Wikipedia.

Nach oben scrollen