Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Functions

The document discusses functions in programming and provides examples of defining, calling, and using functions. It covers key reasons for using functions like modularity, abstraction, and code separation. Syntax for defining functions and common naming conventions are also explained.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Functions

The document discusses functions in programming and provides examples of defining, calling, and using functions. It covers key reasons for using functions like modularity, abstraction, and code separation. Syntax for defining functions and common naming conventions are also explained.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

4/25/24, 10:36 PM Notebook.

ipynb - Colab

keyboard_arrow_down Functions
In programming, a function is a block of organized, reusable code that performs a specific task
or a set of instructions. Functions provide a way to break down complex problems into smaller,
manageable parts, making the code more modular, readable, and maintainable. They play a
crucial role in programming by promoting code reuse and improving overall program structure.

Motivation

Welcome to your journey of learning about functions in programming! Let's explore the key
reasons why learning about functions is valuable:

Modular and Reusable Code: Functions are like special boxes that hold sets of
instructions. When you put instructions inside a function, you can use those instructions
again and again in your program. It's like having a handy tool that you can use whenever
you need it. Functions help you organize your code, avoid repeating the same instructions,
and make your code easier to maintain and grow.

Abstraction and Code Clarity: Functions help simplify complicated things in your code.
They let you give a name to a set of instructions, so you don't have to remember all the
details. It's like using easy words instead of difficult ones. Functions also make your code
easier to understand because you can read the names and know what each part does.
When your code is clear, it's easier for others (including yourself in the future) to
understand and use it.

Code Separation and Modifiability: Functions help you separate and organize your code
into smaller parts. It's like having different sections for different tasks. This makes your
code easier to understand, find problems, and make changes. It's also like having a team of
people working together on different parts of the code. With functions, you can collaborate
with others and make your code better and easier to maintain.

keyboard_arrow_down Function Definition and Syntax


When defining functions, certain syntax rules need to be followed. This section will cover the
syntax for defining functions, naming conventions, and the use of parentheses and colons.

Syntax for Defining Functions


The general syntax for defining a function in Python, is as follows:

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Note… 1/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

def function_name(parameters):
# Function body
# Code to be executed
return value

The def keyword is used to declare a function


function_name is the identifier for your function. Choose a descriptive name that reflects
the purpose of the function
parameters are optional inputs that can be passed to the function
The function body consists of one or more statements that are indented under the function
definition
The return statement, if used, specifies the value that the function should return when it is
called

keyboard_arrow_down Use of Parentheses and Colons


Parentheses and colons play important roles in function definitions:

Parentheses: The parentheses () following the function name are used to enclose the
function's parameters. Parameters are optional and can be used to pass values into the
function.
Colons: The colon : at the end of the function definition is used to indicate the start of the
function body. It is a required part of the syntax and distinguishes the function definition
from the code inside the function.

Now that we understand how to define a function, let's write our first function. Here's an
example:

def calculate_rectangle_area(length, width):


area = length * width

In this example, we define a function called calculate_rectangle_area that takes two


parameters: length and width . Inside the function, we calculate the area of the rectangle by
multiplying the length and width . We store the result in the area variable. In a later section we
will talk more about how we can see the outputs of any function we define.

Naming Conventions for Functions

When naming functions, it is essential to follow certain naming conventions to enhance code
readability and maintain consistency. Here are some common conventions:

Use descriptive names: Choose names that clearly indicate the purpose or action
performed by the function

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Note… 2/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

Use lowercase letters and underscores: For readability, it is common to use lowercase
letters with underscores to separate words in function names (e.g., calculate_area ,
print_report )
Be consistent: Follow a consistent naming convention throughout your codebase to
maintain clarity and make your code more approachable for other developers

Let's look at some examples of good and bad function names following the naming conventions.

Good Examples:

calculate_area : This function name clearly indicates its purpose: to calculate the area of
a shape. It follows the convention of using lowercase letters and underscores to separate
words.
print_report : This function name describes its action: printing a report. It is easy to
understand and follows the convention of using lowercase letters and underscores.
validate_email : This function name indicates that it is used for email validation. It follows
the convention of using lowercase letters and underscores.

Bad Examples:

fn1 : This function name is not descriptive and does not provide any information about its
purpose or action. It does not follow the convention of using descriptive names.
Function_A : This function name uses capital letters and underscores, which is not
consistent with the convention of using lowercase letters. Additionally, the name does not
provide any specific information about the function's purpose.
a - This function name is too short and lacks clarity. It does not follow the convention of
using descriptive names.

Remember, using descriptive and meaningful names for functions enhances code readability
and makes it easier for others (including your future self) to understand and maintain the code.

keyboard_arrow_down Function Calls


Once you have defined a function, you can call it to execute the code inside the function's body.

To call a function, you simply write the function name followed by parentheses
() .

# Function definition
def greet():
print("Hello, there!")

# Function call
greet()

Hello, there!

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Note… 3/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

In this example, the greet function is called by writing its name followed by parentheses. The
code inside the function's body, in this case, printing "Hello, there!" , is executed when the
function is called.

keyboard_arrow_down Function Parameters and Arguments


In functions, parameters and arguments play distinct roles.

Parameters are variables defined in the function declaration, while arguments are
the actual values passed to a function during its call.

Parameters in Functions
Parameters are variables defined in the function's declaration that serve as placeholders for the
values that will be passed to the function when it is called. They define the input requirements
for the function.

def greet(name): # "name" is the parameter


print("Hello, " + name + "!")

In this example, the greet function has one parameter named name . It specifies that the
function expects an argument to be provided when it is called, which will be used to greet the
person by name.

keyboard_arrow_down Different Types of Arguments


Remember, arguments are the actual values passed to a function during its call. There are
different types of arguments you can pass:

Required Arguments: These are arguments that must be provided in the same order as the
function's parameter list. Failure to provide a required argument will result in an error.

def add_numbers(a, b): # "a" and "b" are parameters


return a + b

sum_result = add_numbers(3, 4) # 3 and 4 are the arguments

def divide_numbers(a, b):


return a / b

result = divide_numbers(10) # Error: missing required argument 'b'

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Note… 4/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Users/maya/Desktop/AiCore Work/Content-Projects/Content/units/Essentials/7. Python
programming/6. Functions and function calls/Notebook.ipynb Cell 15 in <cell line: 4>
()
<a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20and%20fu
line=0'>1</a> def divide_numbers(a, b):
<a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20and%20fu
line=1'>2</a> return a / b
----> <a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-

In this example, the divide_numbers function expects two arguments: a and b . The function
divides a by b and returns the result . However, when calling the function, we only provide one
argument ( 10 ) instead of the required two. As a result, the function call raises a TypeError
because the required argument b is missing.

To fix this error, we need to provide both required arguments when calling the function. For
example:

result = divide_numbers(10, 2)
print("The result is:", result)

The result is: 5.0

In this corrected example, we provide both 10 and 2 as arguments when calling the
divide_numbers function. The function executes successfully and returns the correct result,
which is then printed.

Optional Arguments: These arguments are not mandatory and have default values
assigned to them in the function's parameter list. They provide flexibility in function calls.

def greet(name, greeting="Hello"): # "name" and "greeting" are parameters


print(greeting + ", " + name + "!")

greet("Alice") # Uses the default greeting "Hello"


greet("Bob", "Hi") # Overrides the default greeting with "Hi"

Hello, Alice!
Hi, Bob!

So remember, when calling a function, you pass arguments inside the parentheses
based on the function's parameter list. The order of the arguments should match
the order of the parameters.

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Note… 5/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

keyboard_arrow_down Function Return Statements


The primary purpose of a return statement is to terminate the execution of a function and send
a value (or values) back to the caller. It allows functions to produce output or results that can be
used in further computations or assigned to variables.

To return a value from a function, you use the return keyword followed by the value (or
expression) you want to send back. Once a return statement is encountered, the function
immediately exits, and the returned value is passed to the caller.

def add_numbers(a, b):


return a + b

result = add_numbers(3, 4)
print("The sum is:", result)

The sum is: 7

In this example, the add_numbers function returns the sum of two numbers. The return
statement return a + b sends the sum back to the caller, which assigns the returned value to the
result variable.

keyboard_arrow_down Handling Multiple Return Values


Functions in Python can also return multiple values simultaneously. This is typically achieved by
returning a tuple, which is a sequence of values enclosed in parentheses. The caller can then
unpack the returned tuple into individual variables.

def get_name_and_age():
name = "Alice"
age = 25
return name, age

name, age = get_name_and_age()


print("Name:", name)
print("Age:", age)

Name: Alice
Age: 25

In this example, the get_name_and_age function returns both the name and age . By using the
syntax name, age = get_name_and_age() , the returned tuple is unpacked, and the values are
assigned to the respective variables.

Return statements allow functions to produce results and share data with the
calling code. By understanding how to use return statements, return values from

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Note… 6/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

functions, and handle multiple return values, you can effectively utilize functions to
perform computations and retrieve meaningful results.

keyboard_arrow_down Scope and Variable Visibility


In programming, scope refers to the region of a program where a variable is defined and can be
accessed. Understanding scope is essential for determining the visibility and accessibility of
variables.

Local Scope
Local scope refers to the region or context within a program where a variable is defined and
accessible. Variables that are defined within a specific block of code, such as a function, have
local scope and are limited to that block. They cannot be accessed from outside that block or in
other parts of the program.

def my_function():
local_var = 10 # Local variable
print(local_var)

my_function()
print(local_var) # Error: local_var is not defined outside the function

10
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/Users/maya/Desktop/AiCore Work/Content-Projects/Content/units/Essentials/7. Python
programming/6. Functions and function calls/Notebook.ipynb Cell 27 in <cell line: 6>
()
<a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20and%20fu
line=2'>3</a> print(local_var)
<a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20and%20fu
line=4'>5</a> my_function()
----> <a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-

In this example, the local_var is a local variable defined within the my_function function. It is
accessible only within the function's scope. Trying to access local_var outside the function
will result in an error because it is not defined in the global scope.

Local scope provides isolation for variables, meaning that variables with the same
name can be used independently in different functions without conflict. Each
function creates its own local scope, allowing you to reuse variable names without
causing conflicts or interference between functions.

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Note… 7/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

def func1():
x = 10
print(x)

def func2():
x = 20
print(x)

func1() # Outputs: 10
func2() # Outputs: 20

10
20

In this example, both func1 and func2 define a variable named x within their local scope. Each
function can use and manipulate its own x variable without affecting the other. This
demonstrates the isolation and reusability of variables within local scope.

keyboard_arrow_down Nested Functions


Nested functions refer to the concept of defining one function inside another function. These
nested functions have access to variables from their enclosing (outer) function, allowing for
increased modularity and code organization.

def outer_function():
outer_variable = "I am outer"

def inner_function():
inner_variable = "I am inner"
print(inner_variable)
print(outer_variable) # Can access outer_variable defined in the outer function

inner_function()

outer_function()

I am inner
I am outer

In this example, the outer_function defines the outer_variable . Inside the outer_function ,
there is another function called inner_function . The inner_function has access to both its
own local variable inner_variable and the outer_variable defined in the outer function.
However, neither inner_variable nor outer_variable is accessible outside their respective
functions.

keyboard_arrow_down Global Scope


https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Note… 8/20
4/25/24, 10:36 PM Notebook.ipynb - Colab

Global scope refers to the region outside of any function or block where variables are defined.
Variables declared outside of functions, at the top level of a program, have global scope and can
be accessed throughout the program.

global_var = "I am a global variable"

def print_global():
print("Inside the function:", global_var)

print_global()
print("Outside the function:", global_var)

Inside the function: I am a global variable


Outside the function: I am a global variable

In this example, the variable global_var is defined outside of any function, making it a global
variable. It can be accessed both inside and outside functions, allowing you to use its value in
various parts of the program.

Remember, to access a variable, you need to consider its scope. Variables defined
in a local scope are accessible only within the same function, while variables
defined in the global scope can be accessed from any part of the program.

keyboard_arrow_down *args and **kwargs

In Python, *args and **kwargs are special syntaxes used to pass a variable number of
arguments to a function. They allow functions to handle an arbitrary number of positional
arguments ( *args ) and keyword arguments ( **kwargs ).

*arg s: Variable Length Positional Arguments

The args syntax allows a function to accept a variable number of positional arguments. The
name args is a convention, but you can use any valid variable name preceded by an asterisk.

def my_function(*args):
print(*args)

my_function(1, 2, 3) # Passing multiple positional arguments

1 2 3

In this example, the my_function accepts any number of positional arguments.

The arguments are collected into a tuple named args .

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Note… 9/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

keyboard_arrow_down **kwarg s: Variable Length Keyword Arguments

The kwargs syntax allows a function to accept a variable number of keyword arguments. Similar
to *args , the name kwargs is a convention, and you can use any valid variable name preceded
by two asterisks.

def my_function(**kwargs):
for key, value in kwargs.items():
print(key, value)

my_function(name="Alice", age=25) # Passing multiple keyword arguments

name Alice
age 25

In this example, the my_function accepts any number of keyword arguments.

The arguments are collected into a dictionary named kwargs . Within the function,
you can access the key-value pairs in the kwargs dictionary and process them as
needed.

keyboard_arrow_down Using *args and **kwargs together

You can use *args and **kwargs together in a function definition to accept both positional and
keyword arguments.

def my_function(*args, **kwargs):


print(*args)
print(*kwargs.items())

my_function(1, 2, name="Alice", age=25) # Passing both positional and keyword arguments

1 2
('name', 'Alice') ('age', 25)

In this example, the my_function can accept both positional arguments ( 1 , 2 ) as part of *arg s
and keyword arguments ( name="Alice" , age=25 ) as part of **kwargs . You can access and
process both types of arguments separately within the function.

keyboard_arrow_down Passing *arg s and kwargs to functions

To pass multiple arguments to a function using *args and **kwargs , you can use the asterisk or
double asterisks when calling the function.

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Not… 10/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

def my_function(arg1, arg2, **kwargs):


print(arg1, arg2)
print(*kwargs.items())

my_args = (1, 2)
my_kwargs = {"name": "Alice", "age": 25}
my_function(*my_args, **my_kwargs) # Unpacking the arguments using * and **

1 2
('name', 'Alice') ('age', 25)

In this example, the variables my_args and my_kwargs hold the arguments to be passed. The *
operator is used to unpack the values in my_args as positional arguments, while the ** operator
is used to unpack the key-value pairs in my_kwargs as keyword arguments.

keyboard_arrow_down Built-in Functions


Built-in functions are predefined functions that are always available for you to use in Python
programming. These functions are part of Python's syntax, so you don't need to import any
modules to use them. You can read a full list of the built-in functions and their uses here, and
some examples are given below:

len() : Returns the length of a variable, e.g. number of elements in a list, or number of
characters in a string
type() : Returns the type of an object, e.g. for a list, it will return list
map() : Applies a function to every item in an iterable e.g. map(my_function, my_list) will
call my_function on every item in my_list , and will return a map object containing all the
outputs, which can then be converted to a list:

# An example of using the map() function


def square_number(n):
return n ** 2

my_list = [ 1, 2, 3]
squared_list = map(square_number, my_list)
print(f"'squared list' is variable of type {type(squared_list)}")
print(squared_list)
squared_list=list(squared_list)
print(f"'squared list' is now a variable of type {type(squared_list)}")
print(squared_list)

'squared list' is variable of type <class 'map'>


<map object at 0x1203270a0>
'squared list' is now a variable of type <class 'list'>
[1, 4, 9]

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Not… 11/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

The above example defines a function called square_number which returns the square of the
input, and uses the built-in map() function to apply it to each element in my_list . By default,
map returns a map object, so to print the answer we must first convert the variable squared_list
to a list .

keyboard_arrow_down Recursive Functions


Recursive functions follow the principle of recursion, which involves solving a problem by
breaking it down into smaller, similar subproblems until a base case is reached. Recursive
functions typically consist of two components:

Base Case: A condition that specifies when the recursion should stop. It represents the
simplest form of the problem that can be solved directly.
Recursive Case: The part of the function that invokes itself, solving a smaller version of the
problem and making progress towards the base case

Examples of Recursive Functions


Recursive functions can be applied to various problems, such as factorial calculation, Fibonacci
sequence generation, and traversing data structures like trees. Here are two examples to
illustrate the concept:

Factorial Calculation

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

result = factorial(5)
print(result) # Outputs: 120

120

The factorial function calculates the factorial of a number n using recursion. The factorial of a
non-negative integer n is the product of all positive integers less than or equal to n .

Here's a step-by-step explanation of how the factorial function works:

The function takes an argument n , which represents the number for which we want to
calculate the factorial
The function begins with a base case:

If n is 0 , we have reached the base case. In this case, we simply return 1 since the
factorial of 0 is defined as 1

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Not… 12/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

If the base case is not met (i.e., n is not 0 ), the function proceeds to the recursive case
In the recursive case:

The function calls itself with the argument n - 1 . This calculates the factorial of n -
1.
The return statement multiplies the result of the recursive call by n , the current value
The recursive calls continue until the base case is reached (when n becomes 0 ). At that
point, the function starts unwinding the recursion by returning the factorial values in
reverse order.

In the example provided ( result = factorial(5) ), we are calculating the factorial of 5 . The
recursive calls unfold as follows:

factorial(5) calls factorial(4)


factorial(4) calls factorial(3)
factorial(3) calls factorial(2)
factorial(2) calls factorial(1)
factorial(1) calls factorial(0)

At this point, the base case is reached. Since factorial(0 ) returns 1 , the recursion starts
unwinding:

factorial(1) returns the result of factorial(0) * 1 , which is 1


factorial(2) returns the result of factorial(1) * 2 , which is 2
factorial(3) returns the result of factorial(2) * 3 , which is 6
factorial(4) returns the result of factorial(3) * 4 , which is 24
factorial(5) returns the result of factorial(4) * 5 , which is 120

Finally, the calculated value, 120 , is assigned to the result variable and printed ( print(result) ).

keyboard_arrow_down Fibonacci Sequence

def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)

result = fibonacci(6)
print(result) # Outputs: 8

The fibonacci function calculates the n th Fibonacci number using recursion. The Fibonacci
sequence is a series of numbers in which each number (after the first two) is the sum of the two
preceding ones. In this case, we are starting the sequence with 0 and 1 .

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Not… 13/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

Here's a step-by-step explanation of how the fibonacci function works:

The function takes an argument n , which represents the position of the desired Fibonacci
number in the sequence.
The function begins with a base case:

If n is less than or equal to 1 , we have reached the base case. In this case, we
simply return n itself. This covers the first two Fibonacci numbers, 0 and 1 , since
they are equal to their positions in the sequence.
If the base case is not met (i.e., n is greater than 1 ), the function proceeds to the
recursive case
In the recursive case:

The function calls itself twice:

fibonacci(n - 1) : This calculates the Fibonacci number at position n - 1


fibonacci(n - 2) : This calculates the Fibonacci number at position n - 2

The return statement combines the results of the recursive calls by adding them together
The recursive calls continue until the base case is reached (when n becomes 0 or 1 ). At
that point, the function starts unwinding the recursion by returning the Fibonacci numbers
in reverse order.

In the example provided ( result = fibonacci(6)) , we are calculating the 6th Fibonacci number.
The recursive calls unfold as follows:

fibonacci(6) calls fibonacci(5) and fibonacci(4)


fibonacci(5) calls fibonacci(4) and fibonacci(3)
fibonacci(4) calls fibonacci(3) and fibonacci(2)
fibonacci(3) calls fibonacci(2) and fibonacci(1)
fibonacci(2) returns 1
fibonacci(1) returns 1
fibonacci(3) returns the sum of the two previous results: 1 + 1 = 2
fibonacci(4) returns the sum of the two previous results: 1 + 2 = 3
fibonacci(3) has already been computed, so it returns its cached result: 2
fibonacci(5) returns the sum of the two previous results: 2 + 3 = 5
fibonacci(6) has already been computed, so it returns its cached result: 8.

Finally, the calculated value, 8 , is assigned to the result variable and printed ( print(result) ).

Precautions and Avoiding Infinite Recursion

Recursive functions require careful consideration to avoid infinite recursion. Here are some
precautions to take:

Ensure a base case: Every recursive function must have a base case that will eventually be
reached, breaking the recursion

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Not… 14/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

Ensure progress towards the base case: Recursive calls should move the problem closer
to the base case with each iteration. Otherwise, the function may enter an infinite loop.
Test with small inputs: Before applying recursive functions to large inputs, test them with
small inputs to verify correctness and efficiency

It's important to understand the nature of the problem and design the recursive function
accordingly to ensure it terminates and provides the desired results.

Function Best Practices

Writing functions that follow best practices is essential for producing clean, maintainable, and
reusable code. This section outlines some key best practices to keep in mind when working with
functions.

Writing Modular and Reusable Functions


Aim for Single Responsibility: Functions should have a clear and focused purpose. They
should ideally perform a single task or solve a specific problem. This improves readability
and makes functions easier to test and reuse.
Encapsulate Repeated Logic: If you find yourself repeating a certain piece of code in
multiple places, consider encapsulating it in a separate function. This promotes code reuse
and avoids redundancy.
Use Descriptive Names: Choose meaningful and descriptive names for your functions.
This helps to convey their purpose and makes the code more readable.

Keeping Functions Concise and Focused


Avoid Excessive Length: Long functions can be challenging to understand and maintain.
Strive to keep functions concise by breaking them down into smaller, more manageable
parts.
Single Level of Abstraction: Functions should follow a single level of abstraction, meaning
they should either contain high-level logic or low-level implementation details. Mixing both
levels can make functions harder to comprehend.

Avoiding Side Effects within Functions


Minimize External Interactions: Functions should primarily operate on their inputs and
produce a return value. Minimize direct interactions with global variables or external
resources (such as file I/O or network operations) within functions. This helps isolate and
test the function's behavior.
Avoid Mutable Parameters: Modifying mutable parameters (e.g., lists, dictionaries) within a
function can lead to unexpected side effects. If necessary, consider creating local copies
of mutable data structures before modifying them.

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Not… 15/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

keyboard_arrow_down Common Functions Errors and Troubleshooting


SyntaxError

Syntax errors occur when the syntax of a function declaration or its statements is incorrect.

def my_function:
print("Hello, world!")

Input In [24]
def my_function:
^
SyntaxError: invalid syntax

Troubleshooting Tips

Check for missing parentheses, colons, commas, or other required syntax elements:

def my_function():
print("Hello, world!")

Verify that the function name is spelled correctly and follows the naming conventions

keyboard_arrow_down NameError

NameError s occur when you use a variable or function name that is not defined or out of scope.

def my_function():
print(my_variable)

Troubleshooting Tips

Double-check the spelling and capitalization of the variable or function name


Ensure that the variable or function is defined before its usage:

def my_function():
my_variable = "Hello, world!"
print(my_variable)

keyboard_arrow_down TypeError

TypeError s occur when you perform an operation on incompatible data types or pass incorrect
arguments to a function or when a function is called with an incorrect number of arguments.
Let's first look at an example of operations with incompatible data types:
https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Not… 16/20
4/25/24, 10:36 PM Notebook.ipynb - Colab

def divide_numbers(a, b):


return a / b

result = divide_numbers(10, "5")

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Users/maya/Desktop/AiCore Work/Content-Projects/Content/units/Essentials/7. Python
programming/6. Functions and function calls/Notebook.ipynb Cell 59 in <cell line: 4>
()
<a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20and%20fu
line=0'>1</a> def divide_numbers(a, b):
<a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20and%20fu
line=1'>2</a> return a / b
----> <a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20and%20fu
line=3'>4</a> result = divide_numbers(10, "5")

/Users/maya/Desktop/AiCore Work/Content-Projects/Content/units/Essentials/7. Python


programming/6. Functions and function calls/Notebook.ipynb Cell 59 in
divide numbers(a, b)

Troubleshooting Tips

Verify that the arguments passed to the function are of the correct data types and in the
expected order:

def divide_numbers(a, b):


return a / b

result = divide_numbers(10, 5)

And now let's look at an example of calling a function with an incorrect number of arguments:

def divide_numbers(a, b):


return a / b

result = divide_numbers(10)

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Not… 17/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Users/maya/Desktop/AiCore Work/Content-Projects/Content/units/Essentials/7. Python
programming/6. Functions /Notebook.ipynb Cell 63 in <cell line: 4>()
<a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20/Noteboo
line=0'>1</a> def divide_numbers(a, b):
<a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20/Noteboo
line=1'>2</a> return a / b
----> <a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20/Noteboo

Troubleshooting Tips

Verify that you pass the same number of arguments to the function as the number of
parameters in the function definition:

def divide_numbers(a, b):


return a / b

result = divide_numbers(10, 2)

keyboard_arrow_down ValueError

ValueError s occur when a function receives an argument of the correct type but with an invalid
value.

def convert_to_int(value):
return int(value)

result = convert_to_int("abc")

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/Users/maya/Desktop/AiCore Work/Content-Projects/Content/units/Essentials/7. Python
programming/6. Functions and function calls/Notebook.ipynb Cell 63 in <cell line: 4>
()
<a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20and%20fu
line=0'>1</a> def convert_to_int(value):
<a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20and%20fu
line=1'>2</a> return int(value)
----> <a href='vscode-notebook-cell:/Users/maya/Desktop/AiCore%20Work/Content-
Projects/Content/units/Essentials/7.%20Python%20programming/6.%20Functions%20and%20fu
line=3'>4</a> result = convert_to_int("abc")

/Users/maya/Desktop/AiCore Work/Content-Projects/Content/units/Essentials/7. Python


programming/6. Functions and function calls/Notebook.ipynb Cell 63 in
convert to int(value)

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Not… 18/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

Troubleshooting Tips:

Check if the input values meet the requirements specified by the function:

result = convert_to_int("123")

keyboard_arrow_down Recursion Errors


Recursion errors, such as maximum recursion depth exceeded ( RecursionError ), occur when a
recursive function does not have a proper termination condition or the recursion depth is too
high.

def countdown(n):
print(n)
countdown(n - 1)

countdown(5)

Troubleshooting Tips:

Review your recursive function to ensure it has a base case that terminates the recursion:

def countdown(n):
if n <= 0:
return
print(n)
countdown(n - 1)

countdown(5)

Check if the recursive function is invoked with the correct arguments to make progress
towards the base case.

By understanding these common function errors and following the troubleshooting tips, you can
effectively identify and resolve issues in your code, leading to more robust and error-free
functions.

Key Takeaways

Functions are important for organizing code, promoting reusability, and improving
readability
When defining functions, use the def keyword followed by the function name, parentheses,
and a colon

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Not… 19/20


4/25/24, 10:36 PM Notebook.ipynb - Colab

Function calls involve using the function name followed by parentheses, passing
arguments, and optionally assigning return values
Parameters are variables defined in the function declaration, while arguments are values
passed during function calls
Return statements specify the value that a function should return, and functions can return
multiple values

https://colab.research.google.com/github/AI-Core/Content-Public/blob/main/Content/units/Essentials/7. Python programming/6. Functions /Not… 20/20

You might also like