What are functions?
Name a block of work, pass arguments, and return a result you can test.
A function is a named block you can run more than once. You write the steps in one place, then call that name whenever the same job comes up. That keeps scripts short and makes the logic testable.
Define, then call
def ping_summary(ok, total):
percent = (ok / total) * 100
return f"{ok}/{total} hosts up ({percent:.0f}%)"
print(ping_summary(7, 8))The def line names the function and lists parameters. The indented block is the body. A call looks like ping_summary(7, 8) — no colon, and only after the function has been defined.
Parameters versus arguments
Parameters are the names in the definition (ok, total). Arguments are the values you pass at the call site. You can also use keyword arguments (ping_summary(total=8, ok=7)) when the order would be easy to mix up.
Return values
Use return to hand a result back to the caller. If you omit it, Python returns None. Prefer returning data over printing inside helpers, so the same function can feed a log, a test, or a web response.