Skip to content

Functions

Alejandra Rodriguez Garcia edited this page Jul 28, 2022 · 22 revisions

A series of statements which returns some value to a caller. It can also be passed zero or more arguments which may be used in the execution of the body. See also parameter, method, and the Function definitions section.
-- Python docs

imagen

Return

All functions calls return one object, although that object might be None (signifying no value) or a tuple containing multiple elements (signifying multiple values).

If there is no return statement, or just a bare return, the object returned is None.

Sometimes, it's correct to return multiple values from a function. For this, you can return a tuple of values.

Function scope

Keyword arguments

Variadic Arguments

So far, we've seen that arguments can be supplied either by position or by name, and that arguments are matched to named parameters (which might have default values) from the function signature when the function is invoked. What happens if we want to capture an argument that we don't expect?

Variadic parameters are Python's answer to that question. A variadic parameter collection collects excess arguments (that would otherwise go unmatched to a parameter) into a data structure, for the function implementation's use. We'll see two kinds of variadic parameters: variadic positional parameters and variadic keyword parameters.

imagen imagen

Reference

https://docs.python.org/3/tutorial/controlflow.html#defining-functions https://realpython.com/python-return-statement/

Clone this wiki locally