Functions

Mixing positional and keyword arguments

You can mix both fashions if you want - there is only one unbreakable rule: you have to put positional arguments before keyword arguments.

Parametrized functions - more details

It happens at times that a particular parameter's values are in use more often than others. Such arguments may have their default (predefined) values taken into consideration when their corresponding arguments have been omitted.

They say that the most popular English last name is Smith. Let's try to take this into account.

The default parameter's value is set using clear and pictorial syntax:

def introduction(first_name, last_name="Smith"):
    print("Hello, my name is", first_name, last_name)

What happens when Python encounters an invocation like this one below?

function_name(argument)

Let's see:

  • First, Python checks if the name specified is legal (it browses its internal data in order to find an existing function of the name; if this search fails, Python aborts the code);

  • second, Python checks if the function's requirements for the number of arguments allows you to invoke the function in this way (e.g., if a specific function demands exactly two arguments, any invocation delivering only one argument will be considered erroneous, and will abort the code's execution);

  • third, Python leaves your code for a moment and jumps into the function you want to invoke; of course, it takes your argument(s) too and passes it/them to the function;

  • fourth, the function executes its code, causes the desired effect (if any), evaluates the desired result(s) (if any) and finishes its task; finally, Python returns to your code (to the place just after the invocation) and resumes its execution.

Last updated