---
title: Data Analysis Career Accelerator Bootcamp - functions
tags: presentation
slideOptions:
theme: black
transition: fade
parallaxBackgroundImage: ''
---
<style type="text/css">
html, .reveal {
background-color: #000000;
font-family: "Lato";
color: #CFC7BF;
}
ol, ul {
font-size: 3rem;
}
.reveal .slide-background-content {
background-color: #000;
}
.reveal .slides section h1,
.reveal .slides section h2,
.reveal .slides section h3,
.reveal .slides section h4,
.reveal .slides section h5,
.reveal .slides section h6{
font-family: "Lato";
color: #D41B2C;
}
</style>
---
<section data-background-image="https://i.imgur.com/1TQ37c4.png" data-background-size="contain" data-background-opacity="0.2">
<h2 style="text-align: center;line-height: 700px; -webkit-text-stroke: 2px black;" class="fragment fade-left"> Northeastern University </h2>
</section>
---
<h3 style="color:white">Data Analytics</h3>
<h3 style="color:white" class="fragment fade-left"><i>Career Accelerator</i></h3>
---
# Python functions
---
## Objectives
1. Understand the purpose of funcions
2. Be able to write modular code
3. Know the syntax of Python functions
---
## Anatomy of a function
Functions allow creating reusable code and avoiding code repetition, copying and pasting.
---
A function in Python is defined with:
- the `def` keyword
- followed by the function name
- zero or more **arguments** in parenthesis `(),`
- a colon `:` to indicate the start of the function's body.
The body of the function is **indented**.
There is an *optional* `return` statement.
---
The recipe for a function definition in Python:
- `def`: the `def` keyword, telling Python we're about to start a function definition
- a name for the function
- `(`: opening parenthesis
- (optional) the **names** of one or more arguments, separated with `,`
- (optional) the **names** and **values** of one or more default arguments, separated with (`,`) *note: we'll see these in the next section*
- `)` closing parenthesis
- `:` a colon
---
### Examples of function definitions
```python
# A basic function that accepts no arguments and returns nothing.
def hello_world():
print("Hello, World!")
# A function that accepts two arguments, and returns a value
def add_numbers(x, y):
return x + y
```
---
If you do not follow the recipe above function wrong, Python throws a `SyntaxError.`
For example, trying to create a function without the colon `:`:
```python
>>> def hello_world()
File "<stdin>", line 1
def hello_world()
^
SyntaxError: invalid syntax
```
---
Python produces meaningful error messages; read them carefully when debugging.
---
### Indentation
Python knows what code is related to a function by its indentation. In Python, whitespace is a part of the syntax.
---
If you get an `IndentationError,` that means that you didn't correctly indent your code after your function definition. To fix an indentation error, use backspace to get to the colon and start a new line. Most Python IDEs will automatically indent correctly.
```python
# The error you'll see if you didn't indent your function correctly.
>>> def add_numbers(x, y):
... return x + y
File "<stdin>", line 2
return x + y
^
IndentationError: expected an indented block
```
---
## Calling a function
Once you have defined a function, you must call it by its name from the main program.
⚠️ Code inside a function is not executed until the function is called.
---
### Without arguments
<iframe src="https://trinket.io/embed/python/f6da745d10" width="100%" height="600" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>
---
### With arguments
```python
>>> def add_numbers(x, y):
... return x + y
...
>>> add_numbers(3, 5)
8
>>>
```
---
## Coding with functions