Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+9 votes

I started using the def inputs with the same names at first when I was learning functions because it was easier to make the connections in my brain than having a bunch of different variables, but now that I understand functions better, should I be getting away from this habit?

What I mean is this:

def someFunction (variable_one, variable_two):
     variable_one = variable_one * 5
     variable_two = variable_two * 10
     return (variable_one, variable_two)

def main():
     variable_one, variable_two = someFunction(8, 9)
     print(variable_one, variable_two)
main()

notice how I used the same variables in the def part of someFunction as they were named in the body of main. I know that the variables are all localized, but is it still considered "proper style" if I use the same variable names between the two?

asked in CSC201 Spring 2021 by (1 point)

5 Answers

+2 votes
 
Best answer

Good question. There actually isn't a problem with using the exact same variable name in two different functions, as long as it serves the same purpose and stores the same value.

Sometimes doing so makes the code a little more readable.

Why, then, do a lot of my class examples use different variable names for the parameters in the functions, as opposed to the variables that are being passed into those functions?

Mainly because I want students to understand that they really are different variables in each function, even if they happen to have the same names. Otherwise, students see the same name appearing inside both functions and jump to the conclusion that it's the same variable, when it's not. So, my coding choice is for pedagogical reasons.

However, once you are a proficient programmer that understands how variables and parameter passing work, then it's totally fine to use the same variable names in multiple functions, as long as they are really good/accurate/meaningful variable names in both cases.

answered by (3.5k points)
+3 votes

I think as long as you know that those are two different variables, you're fine, but it might be good practice to create a system to differentiate them.

answered by (1 point)
+3 votes

It wouldn't make a difference to the result. But I think using a different variable name for the formal and actual parameter is a good style and gets rid of confusion especially when you have multiple functions getting the same variable name, but performing different tasks.

answered by (1 point)
+3 votes

It wouldn't affect the result of your code. However, using the exactly same variable names is definitely not recommended because the confusion might arise once your code gets more complicated or when you share your codes with other persons.

answered by (1 point)
–3 votes

I tried running your code on thonny, it gives 40, 90 which totally logical, since it multiplies the two variables by 8 and 9, i woudn't work if you don't use the same variables name,

answered by (1 point)
+1

UPDATE: you could just keep them in the same function i guess, could you please let me know why did you separate your code to two different functions in this case?

+2

So the change would be to the someFunction function variables

Insread of

def someFunction (variable_one, variable_two):
         variable_one = variable_one * 5
         variable_two = variable_two * 10
         return (variable_one, variable_two)

it would be:

def someFunction (pumpkins, tallBuildings):
         pumpkins = pumpkins * 5
         tallBuildings = tallBuildings * 10
         return (pumpkins, tallBuildings)

And my question is, even though these two functions would return the same values, is it okay to just stick with the first one (assuming the variables are descriptive enough), or should we make an active effort to use different variables when defining a new function?

+1

im not sure, but when changing them on/ or keeping em the same, nothing changes because the code refers to the name of the variables first not the name of the function , you can see when you debug the code

...