FAQ: Introduction to Functions - Keyword Arguments

Essentially, yes. It’s our design and we want it to be flexible but stable and reliable. Above we set up the condition to accept any number of arguments so long as there is at least one. To make it practical we would have a goal and purpose in mind, of course. And, we don’t want the program raising errors for the user, but for us, during the build and debug phase. From a production standpoint we would have the necessary checks and balances in place so we could report the problem to the user without a fatal error.

def calculation(x, y=1):
print(x * y)
calculation(1)

gives us 1 as the output, since y has a default value. However,

def calculation(x, y=1):
print(x * y)
calculation(1, 2) gives us the output as 2! How is that possible? Did I make a mistake somewhere?

Your default parameter y=1 in the function definition will only be used if you don’t provide an argument for that parameter. If you provide a value then the default is ignored.

By using calculation(1, 2) you have set the parameter x equal to 1 and y equal to 2 and the function returns what you might expect in that case.

1 Like

Can all the parameters in a function be keyword arguments?

Try it…

>>> def second_degree_equation(a=1, b=1, c=1):
    return lambda x: a * x ** 2 + b * x + c

>>> f = second_degree_equation()
>>> f(2)
7
>>> 

The function above returns a function with a, b and c as constants. In Python a lambda is an anonymous function. We could have written it like so,

>>> def second_degree_equation(a=1, b=1, c=1):
    def func(x):
        return a * x ** 2 + b * x + c
    return func

>>> f = second_degree_equation()
>>> f(3)
13
>>> 

For clarity,

We learn in around G. 9 or G. 10 about quadratic equations. They can take various forms, such as one, two, or more terms with degree 2. The one above is the general form of an equation with one second degree term.

y = Ax^2 + Bx + C

where A, B and C are constants, and x is the variable.

Consider Pythagorean Triangle…

a^2 + b^2 = c^2

This gives us the relation of the base and side of a right triangle to its hypotenuse.

A very similar equation describes the relation of the x and y axes to the radius of a circle.

x^2 + y^2 = r^2

The concept of a function returning a function will be coming up in due course so don’t stress over it right now if you don’t quite understand. The key here is that we have shown that we can use all keyword arguments.

2 Likes

im a little bit confused as to why the title in this code has the + marks beside it within quotation marks.

maybe i need to go back and review some of this stuff

print(“Creating a spreadsheet called “+title+” with “+str(row_count)+” rows.”)

Try breaking that statement down into its indiivudal parts to see what goes where so you can work out what is being done and have a little look back at string concatenation.

ill do that now, thanks

I am having a hard time with this lesson on Keyword Arguments. With the last question:
Call create_spreadsheet() with title set to "Applications" and row_count set to 10

I’m not sure what I’m doing wrong.

Looks like the defaults are possibly something else? From the wording it looks like we are expected to supply the arguments in the call…

create_spreadsheet("Applications", 10)
1 Like

Hello!

like this? I feel like i’ve tried every combo of things and it still says it is wrong

Is the row_count default supposed to be set to 1000?

1 Like

It says 10 in the instructions, I’m not sure where 1000 came from

As the default.

def create_spreadsheet(title, row_count=1000):
1 Like

Ohhh, earlier in the assignment I did set it to 1000. I must have deleted it when I changed it to 10. I see. Just wondering why I would want to change it to 10 when I call it rather than just change the default? is it for convenience?

Thank you for your help

1 Like

Yes. If we want the default size we only need to supply the title for the new worksheet.

1 Like

How come these lines of code in keyword arguments, sorry had to copy and paste:

def create_spreadsheet(title, row_count = 1000):
print("Creating a spreadsheet called “+title+” with " + str(row_count) + " rows ")

Call create_spreadsheet() below with the required arguments:

create_spreadsheet(“Downloads”)
create_spreadsheet(“Applications”, 10)

Running it gives me this, and spits it out twice instead of once:
Creating a spreadsheet called Downloads with 1000 rows
Creating a spreadsheet called Applications with 10 rows

Question is why does it run it twice when from my understanding of the code it really should just do it once like:
Creating a spreadsheet called Downloads with 10 rows

Am I wrong on assuming that or is it somewhere in the coding calling on it twice

me again is (“downloads”) calling on the first line
and (“Applications”, 10) calling the second line resulting in both lines printed out,

Please check the following FAQ which covers amongst other things how to format code on the forums (Python without indentation is hard to understand so please use formatted code in the future).

Your function is run every time you call it function(arguments). So your assumption about using it on both lines is correct, for this function you would receive output every time you called it.

that’s code academy code, I copy and pasted from their own view solution, but thanks for the FAQ