liveBook · Manning

Return value

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

A return value is the result that a function sends back to the code that called it.

A return value is the result a function sends back to the code that called it. Calling a function runs its code; storing the returned result in a variable lets you use it later. section 3.1.2 section 4.3.1

When working with return values:

  • Pass arguments that match the function’s expected parameter types. section 3.1.2
  • Assign the result to a variable when you need to use it later; otherwise, it can be lost. section 3.1.2
  • Test different input categories, including negative, zero, and positive outcomes. section 3.3.1

Calculating money made

The function `money_made(10, 15, 17)` returns `20`. With a current price of 15, it returns `0`; with a current price of 15 after purchasing at 17, it returns `-20`. These cases show that a return value can represent profit, no change, or loss. section 3.3.1

    return num_shares * (current_share_price - purchase_share_price)

This listing shows a return statement sending a calculated expression back to the caller. section 3.3.1

    password = input("Enter a strong password: ")
    while not is_strong_password(password):            #1
        password = input("Enter a strong password: ")
    return password

This listing shows a function returning the password only after repeated input satisfies the strength check. section 3.3.3

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

How deep it goes
Explains calls, arguments, assignment, and test categories, but stops short of implementation internals.
Examples
Shows larger, money_made, max, input, and password-validation functions returning values.
What it assumes
Assumes no advanced programming background and teaches function calls, arguments, variables, and outputs through examples.
What you can do after
Apply it via Python by capturing function results, inspecting outputs, and testing varied inputs.
How it connects
Connects return values with functions, parameters, arguments, variables, input, and testing.

Also explained in

Compare how 8 books explain return value →

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.