liveBook · Manning

Function declaration

As explained in Learn AI-Assisted Python Programming, Second Edition

A function declaration is a source-language construct that specifies a function’s name and parameter interface, and may include its implementation.

A function declaration defines a named, reusable operation. Treat it as a promise: provide the expected inputs, and the function returns the appropriate output without requiring you to know its internal mechanics. section 3.1.1

In Python, the declaration begins with a function header. The header uses def, gives the function a name, lists its inputs, and ends with a colon. The body contains the work and return statements can provide the result. section 3.1.1

# write a function that returns the larger of two numbers
# input is two numbers
# output is the larger of the two numbers
def larger(num1, num2):          #1
    if num1 > num2:        #2
        return num1        #2
    else:                  #2
        return num2        #2

This example declares larger, compares two numbers, and returns the larger value. Comments can describe the desired inputs and output before code generation. section 3.1.1

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Explains function headers through their keyword, name, inputs, colon, body, and returned output.
Examples
Uses a larger function that compares two numbers, alongside the mathematical example f(x) = x².
What it assumes
Assumes familiarity with comments as Copilot prompts while teaching function headers and input-output behavior.
What you can do after
Build it from scratch by writing a Python function header, defining its body, and returning a result.
How it connects
Connects function declarations with function headers, inputs, outputs, return statements, and comments.

Also explained in

Compare how 6 books explain function declaration →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.