Programming words

What is a function?

A function is the name of a job. When you call the name, that job runs again. You do not copy the same code everywhere.

  • You can pass data in, such as a city name.
  • When the job ends it can return a result, such as the time.
  • A clear name makes the code readable.

hello() writes the same sentence every time you call it. If you pass a name in, the sentence changes with that name. A function does not run by itself. The line that calls it runs it.

Related guides

About this tool

A function is a named block of code that performs a specific task. When you call its name, the code runs again. Functions avoid duplicating the same code throughout your program, keeping it clean and readable.

You can pass data into a function as parameters, allowing the function to behave differently each time. After completing its work, a function can return a result, such as the current time or a calculation outcome.

How to use it

  1. Choose a descriptive name and declare the function using `def` (Python) or `function` (JavaScript).
  2. Write the operations inside the function and use `return` to output a value.
  3. Call the function by its name and provide any required arguments.
  4. Print or log the returned value to see the result.

Common uses

Frequently asked questions

What is a function?

A function is a named block of code that performs a specific task.

How do you call a function?

You call it by writing its name and passing any needed arguments, e.g., `myFunc(5)`.

What does a function return?

A function returns a value using `return`, which can then be used by the calling code.