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.
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.
Functions can also take in information, which we call inputs or parameters. The function processes this input and can return a helpful result.
✏️ 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.
Step 1: Define the function and give it a clear name. We will write: function doubleNumber(number).
Step 2: Create a variable inside the function to hold the new value. We can write: let result = number * 2.
Step 3: Return the final value so the program can use it. We write: return result.
Step 4: Call the function with an input. If we run doubleNumber(5), the function returns 10.
Want the full interactive lesson — quiz, animations, and Finn cheering them on?