---
# System prepended metadata

title: 'Python Functions: Fundamentals'
tags: [python]

---

# Python Functions: Fundamentals

> Functions help us work faster and simplify our code. Whenever we have a repetitive task, we can considerably speed up our workflow by using a function each time we do that task.

Python, however, doesn't have built-in functions for absolutely every task we might want to do. For instance, in the previous mission, we generated many frequency tables for our iOS apps data set, and we might want to use a function to accomplish that repetitive task. The function should:

* Take the iOS apps data set in as input
* Generate the frequency table for the column we want
* Return the frequency table (in the form of a dictionary) as output

> Started with the def statement, where we:
> 
> Specified the name of the function (square)
> Specified the name of the variable (a_number) that will serve as input
> Surrounded the input variable a_number within parentheses
> Ended the line of code with a colon (:)
> Specified what we want to do with the input a_number (in the code below the def statement)
> 
> We multiplied a_number by itself: a_number * a_number
> Then we assigned the result of a_number * a_number to a variable named squared_number
> Ended with the return statement, where we specified what we want returned as the output.
> 
> The output is the variable squared_number, which stores the result of a_number * a_number.

![](https://i.imgur.com/dKJZgop.png)

test

```python=
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

def extract(index):
    column = []
    for row in apps_data[1:]:
        column.append(row[index])
    return column

genres = extract(11)
```
```
Write a function named freq_table() that generates a frequency table for any list.

The function should take in a list as input.
Inside the function's body, write code that generates a frequency table for that list and stores the table in a dictionary.
Return the frequency table as a dictionary.
Use the freq_table() function on the genres list (already defined from the previous screen) to generate the frequency table for the prime_genre column. Store the frequency table to a variable named genres_ft.

Feel free to experiment with the extract() and freq_table() functions to easily create frequency tables for any column you want.
```

```python=
# CODE FROM THE PREVIOUS SCREEN
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

def extract(index):
    column = []    
    for row in apps_data[1:]:
        value = row[index]
        column.append(value)    
    return column

genres = extract(11)

def freq_table(column):
    frequency_table = {}
    for value in column:
        if value in frequency_table:
            frequency_table[value] += 1
        else:
            frequency_table[value] = 1
    return frequency_table

genres_ft = freq_table(genres)
        
```

when we use subtract(10, 7) or subtract(7, 10), we're not explicit about what arguments correspond to what parameters. To solve this ambiguity, Python maps arguments with parameters by position; the first argument will be mapped to the first parameter, and the second argument will be mapped to the second parameter.

![](https://i.imgur.com/EYo7dQE.png)

> Arguments that are passed by position are called positional arguments. In the diagram above, we can see the order we use to pass in positional arguments makes a clear difference in terms of argument-parameter mapping, leading to different results in each example (in the example on the left the result will be 3, while on the right the result will be -3).

test
```python=
opened_file = open('AppleStore.csv')
from csv import reader
read_file = reader(opened_file)
apps_data = list(read_file)

def extract(data_set, index):
    column = []    
    for row in data_set[1:]:
        value = row[index]
        column.append(value)    
    return column

def find_sum(a_list):
    a_sum = 0
    for element in a_list:
        a_sum += float(element)
    return a_sum

def find_length(a_list):
    length = 0
    for element in a_list:
        length += 1
    return length

def mean(data_set, index):
    column = extract(data_set, index)
    return find_sum(column) / find_length(column)

avg_price = mean(apps_data, 4)
```

> There's a lot of code running in the background when we run a single line of code like mean(apps_data, 4). Running mean(apps_data, 4) will first call the mean() function, which in turn will need to call the extract(), find_sum(), and find_length() functions in order to work properly.

![](https://i.imgur.com/PGaSIHA.png)

:::success
In programming, errors are known as bugs. The process of fixing an error is commonly known as debugging.
:::



###### tags: `python`

