Function

A function is a reusable block of code designed to perform a specific task. 

It helps organize programs into smaller, manageable parts.

Why functions are important

Functions allow you to:

  • Avoid repeating code

  • Organize logic clearly
  • Reuse code multiple times
  • Simplify complex programs

Instead of writing the same code again, you can call a function.

How a function works

A function usually has:

  1. A name
  2. Optional parameters (inputs)
  3. A block of code
  4. A return value (optional)

Basic structure:

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

To execute it:

greet()

Function with parameters

Functions can receive input values.

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

Calling the function:

greet("John")

Output:

Hello John

Return value

A function can return a result.

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

Using the result:

result = add(5, 3)

Here, result becomes 8.

Why learning functions matters

Functions help you:

  • Build structured programs
  • Improve readability

  • Reduce errors

  • Create modular code

Large programs are built from many small functions.

Simple example

Think of a function like a coffee machine: You press a button (call the function), it performs a task, and gives you a result.

Related terms

Source

Simplified from general programming documentation and Wikipedia.

Nach oben scrollen