Skip to content
Permalink
689d0e07e4
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
49 lines (38 sloc) 2.09 KB

Functions

Functions are the way in which we encapsulate code so that it can be reused multiple times. Sometimes this is in a single program and other times it's to include inside our own custom library.

Any time code needs to be executed or evaluated in the same way, it can also be passed into a function to have the same process done to it. By passing functions to functions we can build up any complex arrangement you can think of, and is how all modern programs are constructed.

Some functions inside python are built in functions that do a specific job or exhibit a specific behaviour. There are also ways to perform small inline functions in this manner. This will be covered in the built in functions section. Let's look at some of the reasons we use functions in more depth:

Scope

Scope is the technical term for if python can see a variable or function at any point in your program. As scope is one of those things that makes more sense when you see it, consider this example:

{{ code_from_file("functions/intro-example.py", 2, 20, execute=true) }}

!!! Example In this example, you can see that we have created a variable named "variable_1" and we have created a function using the key word "def", for more information on creating variables you can find it here in the user functions section.

Abstraction and reusability

Modularity

Outline

outline user defined - abstraction and reusability - modularity - namespace separation - how a function is called and how it is defined - passing arguments - keyword argument rules - mutable default parameters - pass by value and pass by reference - return statement - side effects - variable length argument lists - tuple packing and unpacking - dictionary packing and unpacking - docstrings - dunders - function annotations

outline built-in functions - generators - yield - lambda - map - Special mentions - any - exec - print