Quick Function Question

Pretty straight forward question. When do we need to define a parameter in a function and when do we not need to?

Screen Link: https://app.dataquest.io/m/316/functions%3A-intermediate/9/scopes-global-and-local

Your Code: def exponential(x):
e = 2.72
print(e)
return e**x

result = exponential(5)
print(e)

In this function, why do we need to include the x in the def exponential(x): compared to other times when we define a function where we could just leave the def exponential() empty.

If left empty (def exponential():) I get this error:

  • TypeErrorTraceback (most recent call last) in () 8 return e**x 9 —> 10 result = exponential(5) 11 print(e) 12 TypeError: exponential() takes 0 positional arguments but 1 was given

Also in this section: https://app.dataquest.io/m/316/functions%3A-intermediate/7/more-about-tuples

Can somebody explain to me how this part of the solution:

  • apps_data, header = open_dataset()

is working step by step?

The problem " Use the open_dataset() function to open the AppleStore.csv file, which has a header row" confuses me because don’t I need to use the open_dataset to get the apps_data?

1 Like

I’m going to answer your first question, about when functions need parameters and when they don’t. The first thing to know is that it’s a bit of a design choice, there are cases where you can make the same function with or without parameters. When you choose to make it with parameters, you make the function more flexible. Put another way, you let the person calling the function provide some extra information on how the function will be run.

Let’s look at the example you provided (I removed the print statement for simplicity):

def exponential(x):
    e = 2.72
    return e**x

This function returns the value of 2.72 raised to the power of the x parameter you provide. If we defined it without the parameter, it would have to look something like this

def exponential():
    e = 2.72
    x = 2
    return e**x

We’d need to make a change, and give x a value inside the function. That would mean that the function would always return 2.72 raised to a fixed number (in my example it’s 3), because the function doesn’t accept any arguments.

Similarly, to call that function we can’t call it with any arguments, or you get the error you saw before.

As you can see, the function without the parameter isn’t as flexible as the one that takes a parameter, because it does the same thing every time. Similarly, if we created the function with two parameters, it becomes even more flexible:

def exponential(e, x):
    return e**x

Now we can call it with any two values, like so:

exponential(2.42, 5)

I hope this helps to explain why you might make different choices about parameters with functions. The important thing to remember is that unless you provide function parameters with default values (you’ll learn more about that shortly if you haven’t already), you must provide the same number of parameter values when you call a function as you set up when you defined it.

4 Likes

To answer your second question,

def open_dataset(file_name=‘AppleStore.csv’, header=True):
opened_file = open(file_name)
from csv import reader
read_file = reader(opened_file)
data = list(read_file)

if header:                  #POINT OF INTEREST IN YOUR QUESTION
    return [data[0], data[1:]]
else:
    return [data]

header, apps_data = open_dataset()

In the if clause you see that ‘if the dataset has a header’ then it returns a list containing the header as the first element, and the rest of the data set as the second element.

Now since ‘Applestore.csv’ has a header, in addition to the fact that open_dataset() has default arguments as ‘Applestore.csv’ and header = True, the output of open_dataset() will be:

[ [header], [rest of the dataset]]

Now, an an example of single line variable assignment=
*( If I write *

*a,b = [1,2] *

This means a = 1, b = 2
This helps is because otherwise we’d have to assign a & b individually in separate lines of code.

similarly,)

header, apps_data = open_dataset()

that is,

header, apps_data = [ [header] , [rest of data] ] #which is basically the output of open_dataset()

1 Like

oh wow that make a ton of sense. thank you!

1 Like

So glad I could be of help! :slight_smile: