Python Python Basics All Together Now Cleaner Code Through Refactoring

Klaudija Ljevar
Klaudija Ljevar
4,476 Points

Confused about num_tickets and number_of_tickets

I need some help understanding. Why did Craig named: amount_due = calculate_price(num_tickets) and than in function it was different: def calculate_price(number_of_tickets). How come it still works , if you know what I mean?

7 Answers

Nemanja Savkic
Nemanja Savkic
14,877 Points

I understand it as just a name for the argument and it should be the same in the return line. You can then change the argument that you post inside. def func(x): return x * 3

func(4)

Alex Kravchenko
Alex Kravchenko
1,270 Points

@Ryan and Anthony Bednarek, this may be too late but I was confused about this as well. When defining the function, you are putting in the parameters into those parenthesis. It is not the same variable that's in your code. You can name it whatever you want, you just have to put it there and then do something with those parameters below in the function. Once you are using the function in your code, the variables you are putting into the function are arguments. So the function would have worked fine if it was

def calculate_price (mickey_mouse)
    return (mickey_mouse * TICKET_PRICE) + SERVICE_CHARGE
#then when the function is called:

final_price = calculate_price (num_of_tickets)

#num_of_tickets is what gets passed into the function 
#is the mickey_mouse parameter which gets multiplied by TICKET_PRICE plus SERVICE_CHARGE 
#and returns the total price. 

The name you put into the parameters doesn't have to be logical or have anything to do with anything. You just put in the most obvious thing you can so you can know what the function is doing and which arguments (variables) will be passed into it. When using your functions, you can put whatever variables in it and it will be what goes into those parenthesis and ran within the function. I don't know if that made sense.

Mustafa Başaran
Mustafa Başaran
28,018 Points

Hi Klaudijja, The logic is the same as simple algebra. A function may take an argument: f(x) and x may take a value: 4 so, it becomes f(4) Right?

Klaudija Ljevar
Klaudija Ljevar
4,476 Points

Thank you, Mustafa. Think it's clearer now :)

I think she means he has used the different names for the same variable.

Anthony Bednarek
Anthony Bednarek
827 Points

Mustafa's answer doesn't really explain why he created both. Mine runs perfectly fine with just the num_of_tickets.

I don't really understand why the variable or argument had to change from num_tickets to number_of_tickets either. It does make it confusing. Anthony, You are correct. Mustafa didn't answer the question of why it changed. Why did it change?

I think Alex explained it better. Its the parameter (that goes in the brackets when you define a function), they dont matter, but the argument (that goes in when you call the function) does I think. The teacher should have explained this better. People here are calling these arguments, but they are parameters.

The paramaters are given in the function... def fun(parameter1) are then passed down to the argument when you call the function....: fun(num_tickets)

It's something that has confused me as well. If you happen to get an answer can you please post it here?

Klaudija Ljevar
Klaudija Ljevar
4,476 Points

You cannot see the answer from above? From Mustafa?

Hi everybody, the name of the argument could be anything, it would just work inside the function locally. If you want you can keep the same name as well. no issues But the real question is there is no need of argument. The program would work if we do not pass the argument. Why would he do that??? :(

Would be better if the lecturer could explain the difference between paramenters and arguments. (parameters get passed down to the function when you call the function).