# Iterating over a list in a function. Confiused

#1

Hi,
I am kinda confiused with this task.
I thought I learnt that there is no point to make a function to be able to access only one list?.
Why are people given an answer to navigate to list directly in function (n)? as example below

``````n = [3, 5, 7]

def total(n):
result = 0
for i in n:
result = result + i
return result

Is this not a proper way of doing it instead?
n = [3, 5, 7]

def total(x):
result = 0
for i in x:
result = result + i
return result
print total(n)``````

#2

the list (`n`) and the function parameter (`n`) just have the same name, the code still works correctly:

``````n = [3, 5, 7]

def total(n):
result = 0
for i in n:
result = result + i
return result

print total(n)
print total(1,2,3)``````

#3

It does work correctly but I would not be able to use this function for another list. So what is the point of it?
Am I getting something wrong within my thinking?

#4

Ohh I know what you mean by it now.
I got confiused by using the same name.
So in that case parameter is not navigating to list itself? Am I right?

#5

Did you run the code? both function calls produce the correct output

because a function parameter has a local scope, it can have the same name as a global variable (the list in this case)

exactly, although i think some learners get confused and think they loop over the global list while they don't

#6

This is exactly what happened in my case.
Thank you so much for clearing this for me.

#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.