0Pricing
PHP Academy · Lesson

What Are Functions?

Understand the purpose of functions and how to define them.

What Are Functions? is a free PHP Academy lesson on CoddyKit — lesson 1 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

What Are Functions?

Welcome to the world of PHP functions! In this lesson, you’ll learn what functions are, why they are useful, and how they make your code more efficient and reusable. Let’s get started!

What Are Functions? — illustration 1

2

Definition of a Function

A function is a block of code that performs a specific task. Functions allow you to write reusable code, making your programs more efficient and easier to maintain.

Example: You can use a function to calculate the area of a rectangle instead of rewriting the same code multiple times.

3

Why Use Functions?

Functions are essential because they:

  • Reduce code duplication by reusing the same block of code.
  • Make your code easier to read and debug.
  • Organize your program into logical sections.

Key Point: Functions improve both the structure and efficiency of your code.

4

Defining a Function

To define a function in PHP, use the function keyword followed by the function name and parentheses. Example:

Note: Function names should be meaningful and follow the naming conventions.

<?php
  function sayHello() {
    echo "Hello, PHP!";
  }
?>

5

Calling a Function

After defining a function, you can call it by using its name followed by parentheses. Example:

<?php
  function sayHello() {
    echo "Hello, PHP!";
  }

  // Call the function
  sayHello();
?>

6

Function Parameters

Functions can accept parameters to perform tasks dynamically. Example:

<?php
  function greet($name) {
    echo "Hello, $name!";
  }

  greet("Alice"); // Outputs: Hello, Alice!
  greet("Bob");   // Outputs: Hello, Bob!
?>

7

Returning Values from Functions

Functions can return values using the return keyword. Example:

Key Point: Use return values to store the result of a function for later use.

<?php
  function add($a, $b) {
    return $a + $b;
  }

  $result = add(5, 10);
  echo $result; // Outputs: 15
?>

8

Best Practices for Functions

Here are some tips for writing effective functions:

  • Use meaningful names that describe what the function does.
  • Keep functions short and focused on a single task.
  • Document the purpose and parameters of your functions using comments.

Tip: Modularize your code by breaking complex tasks into smaller functions.

9

<?php
  function multiply($a, $b) {
    return $a * $b;
  }
?>

10

Great Job!

Congratulations! You’ve learned what functions are, how to define them, and how to use parameters and return values. Functions are a fundamental part of PHP programming and will help you write better, more efficient code. In the next lesson, we’ll explore built-in functions in PHP. Let’s keep coding!

What Are Functions? — illustration 10

Frequently asked questions

Is the “What Are Functions?” lesson free?

Yes — the full text of “What Are Functions?” is free to read here on the web, and the PHP Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “What Are Functions?”?

Understand the purpose of functions and how to define them. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 5, so you can start here or from the beginning and move at your own pace.

How long does the “What Are Functions?” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Are Functions?
  2. Built-in Functions
  3. User-Defined Functions
  4. Passing Arguments to Functions
  5. Return Values
← Back to PHP Academy