Studyfin Studyfin ✨ Sign up free

Functions

Grade 8 · Coding · Free lesson

Hi! I am Studyfin, your AI tutor. Today, we will learn how functions help us write clean, smart code!

In coding, a function is a named block of code that does a specific job. Instead of writing the same steps over and over, you write them once inside a function.

school

Think of a function like a recipe. You define the steps once, and then you can 'call' or run that recipe whenever you need it by using its name.

myFunction()

Functions can also take in information, which we call inputs or parameters. The function processes this input and can return a helpful result.

FunctionInputOutput
✏️ Worked example

Create a function called 'doubleNumber' that takes a number, multiplies it by 2, and returns the result. Let us walk through how this works step-by-step.

  1. Step 1: Define the function and give it a clear name. We will write: function doubleNumber(number).
  2. Step 2: Create a variable inside the function to hold the new value. We can write: let result = number * 2.
  3. Step 3: Return the final value so the program can use it. We write: return result.
  4. Step 4: Call the function with an input. If we run doubleNumber(5), the function returns 10.
to learn
Want the full interactive lesson — quiz, animations, and Finn cheering them on?

More Coding lessons