### Stats

2110 Wednesday, June 21, 2017
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com

# 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

ParameterDetails
arg1, ..., argNRegular arguments
*argsUnnamed positional arguments
kw1, ..., kwNKeyword-only arguments
**kwargsThe rest of keyword arguments

## Remarks

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):
return a + b
``````
• 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
``````
``````def outer_fun(name):