Python Functions

Python Functions

Python supports functions like other programming languages. Unlike other languages, Python requires whitespace and special keyword to create functions. Now, what are functions? Let’s take a look.

 

Functions

Functions are small composed groups of instructions that perform a single task. These functions can also take parameters to either modify or use to perform the function’s task. In Python, these can be more complex. The examples below showcase the power of Python functions by using three special properties of Python functions parameters. Keyword arguments/parameters, default parameters, and variable length parameters. here are some example functions.

 

Console Output

 

The Explanation

Example one is your standard function that takes arguments. Each argument must be placed in the proper order; if you don’t provide an argument, the function will raise an error.

Example two has default parameters; these parameters will be used if no value is provided for that parameter. The function calls use keyword parameters to specify the value of the parameter regardless of the order in which they appear.

Example three uses variable length parameter; this means it will take any number of parameters. This is useful when you don’t know how many items your function might take.

 

These are some of the basic parameters of Python functions and how they work; next time we will be going over classes. Stay tuned!

Leave a Reply

Your email address will not be published.