Python Language Tutorial => Functions
Introduction
Functions in Python provide organized, reusable and modular code to perform a set of specific actions. Functions simplify the coding process, prevent redundant logic, and make the code easier to follow. This topic describes the declaration and utilization of functions in Python.
Python has many built-in functions like print(), input(), len(). Besides built-ins you can also create your own functions to do more specific jobs—these are called user-defined functions.
Syntax
- def function_name(arg1, ... argN, *args, kw1, kw2=default, ..., **kwargs): statements
- lambda arg1, ... argN, *args, kw1, kw2=default, ..., **kwargs: expression
Parameters
| Parameter | Details |
|---|---|
| arg1, ..., argN | Regular arguments |
| *args | Unnamed positional arguments |
| kw1, ..., kwN | Keyword-only arguments |
| **kwargs | The rest of keyword arguments |
5 basic things you can do with functions:
-
Assign functions to variables
def f(): print(20) y = f y() # Output: 20 -
Define functions within other functions (Nested functions )
def f(a, b, y): def inner_add(a, b): # inner_add is hidden from outer code return a + b return inner_add(a, b)**y -
Functions can return other functions
def f(y): def nth_power(x): return x ** y return nth_power # returns a function squareOf = f(2) # function that returns the square of a number cubeOf = f(3) # function that returns the cube of a number squareOf(3) # Output: 9 cubeOf(2) # Output: 8 -
Functions can be passed as parameters to other functions
def a(x, y): print(x, y) def b(fun, str): # b has two arguments: a function and a string fun('Hello', str) b(a, 'Sophia') # Output: Hello Sophia -
Inner functions have access to the enclosing scope (Closure )
def outer_fun(name): def inner_fun(): # the variable name is available to the inner function return "Hello "+ name + "!" return inner_fun greet = outer_fun("Sophia") print(greet()) # Output: Hello Sophia!
Additional resources
- More on functions and decorators: https://www.thecodeship.com/patterns/guide-to-python-function-decorators/