Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Python Functions

Goal

  • To understand defining and implementing python functions

  • Work with function and return types

  • Create and Pass Lamda functions for basic operations

Python Functions

a function is a units of logic to complete a task.Functions onlyrun on when it was called. In Python to define a function, we start with “def” followed by function name and braces for the input params

Example

def add(a,b):
    return a+b
print(add(1,3))

Functions ~ Variables Scope

## Functions and Variable Scope.
## By default, variables assigned inside a function are local to that function.
## To modify a variable outside of the current scope, use the 'global' keyword.
a = 10
def unchange_ref():
    a = 20
    print("Inside function:", a)
unchange_ref()
print("Outside function:", a)

def unchange_ref():
    global a
    a = 20
    print("Inside function:", a)

unchange_ref()
print("Outside function:", a)
Inside function: 20
Outside function: 10
Inside function: 20
Outside function: 20

Function ~ Return Types

## Return None from a Function
def greet(name):
    print("Hello,", name)
result = greet("Alice")
print(result)

## Return Single Value from a Function
def square(num):
    return num * num
result = square(5)
print(result)

## Returning Multiple Values from a Function
def get_values():
    return 10, 20, 30

x, y, z = get_values()
print(x, y, z)

Functions ~ Return Reference

as we know each function keeps its own copy of the variable inside the memory table, when a function creates a Object and returns its reference,it can mutate the data via the reference.

def ref_function():
    lst = []
    return lst
my_list = ref_function()
print("Before mutation:", my_list)
my_list.append(4)
print("After mutation:", my_list)
Before mutation: [1, 2, 3]
After mutation: [1, 2, 3, 4]

Default Argument Values

The most useful form is to specify a default value for one or more arguments. This creates a function that can be called with fewer arguments than it is defined to allow

## Default Argument Values
def greet(name, msg="Hello"):
    print(msg, name)
greet("Alice")
greet("Bob", "Hi")
greet(name="Charlie", msg="Welcome")
greet(msg="Good Morning", name="David")
greet("Eve", msg="Greetings")

Important warning:

The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes. For example, the following function accumulates the arguments passed to it on subsequent calls:

#### Important warning: Example 1.
def f(a, L=[]):
    L.append(a)
    return L

print(f(1))
print(f(2))
print(f(3))
[1]
[1, 2]
[1, 2, 3]

Keyword Arguments

Functions can also be called using keyword arguments of the form kwarg=value. For instance, the following function:

def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
    print("-- This parrot wouldn't", action, end=' ')
    print("if you put", voltage, "volts through it.")
    print("-- Lovely plumage, the", type)
    print("-- It's", state, "!")

parrot(1000)                                          # 1 positional argument
parrot(voltage=1000)                                 # 1 keyword argument
parrot(voltage=1000000, action='VOOM')               # 2 keyword arguments
parrot(action='VOOM', voltage=1000000)               # 2 keyword arguments
parrot('a million', 'bereft of life', 'jump')        # 3 positional arguments
parrot('a thousand', state='pushing up the daisies') # 1 positional, 1 keyword
-- This parrot wouldn't voom if you put 1000 volts through it.
-- Lovely plumage, the Norwegian Blue
-- It's a stiff !
-- This parrot wouldn't voom if you put 1000 volts through it.
-- Lovely plumage, the Norwegian Blue
-- It's a stiff !
-- This parrot wouldn't VOOM if you put 1000000 volts through it.
-- Lovely plumage, the Norwegian Blue
-- It's a stiff !
-- This parrot wouldn't VOOM if you put 1000000 volts through it.
-- Lovely plumage, the Norwegian Blue
-- It's a stiff !
-- This parrot wouldn't jump if you put a million volts through it.
-- Lovely plumage, the Norwegian Blue
-- It's bereft of life !
-- This parrot wouldn't voom if you put a thousand volts through it.
-- Lovely plumage, the Norwegian Blue
-- It's pushing up the daisies !
def cheeseshop(kind, *arguments, **keywords):
    print("-- Do you have any", kind, "?")
    print("-- I'm sorry, we're all out of", kind)
    for arg in arguments:
        print(arg)
    print("-" * 40)
    for kw in keywords:
        print(kw, ":", keywords[kw])

cheeseshop("Limburger", "It's very runny, sir.",
           "It's really very, VERY runny, sir.",
           shopkeeper="Michael Palin",
           client="John Cleese",
           sketch="Cheese Shop Sketch")
-- Do you have any Limburger ?
-- I'm sorry, we're all out of Limburger
It's very runny, sir.
It's really very, VERY runny, sir.
----------------------------------------
shopkeeper : Michael Palin
client : John Cleese
sketch : Cheese Shop Sketch

Lambda Expressions

Lambda functions can be used wherever function objects are required,Small anonymous functions can be created with the lambda keyword

def make_incrementor(n):
    return lambda x: x + n

f = make_incrementor(42)
print(f(0))
print(f(1))
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
pairs.sort(key=lambda pair: pair[1])
pairs
42
43
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]