0% complete

Functions · Lesson 2 of 9

Creating Functions

Declare a function and call it with parentheses.

Lesson

Declare a function with the function keyword, a name, parentheses, and a body. A declaration does not run the body; calling the function does. Prefer one clear responsibility per function and place related operations together in a class or module as an application grows. This function converts minutes into seconds and returns the calculated value. Further guidance Treat a function as a small contract: name its inputs, state what it returns, and avoid hidden dependencies. Test a representative value and an edge case. Keep a function focused; callers should decide how to display a returned result or handle a reported failure. Practice task: Create a fresh example for Creating Functions. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.

Code examples

Creating Functions
PHP
function toSeconds(int $minutes): int
{
    return $minutes * 60;
}
echo toSeconds(3);
Expected output
180

Key points

  • Declare a function and call it with parentheses.
  • Keep inputs, return values, and side effects explicit.
  • Prefer focused functions that are easy to test independently.

Your learning progress

Finish this lesson

Optional practice

PHP playground

This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.

Execution is disabled. No code is sent to a server.