Please help with function logic!


I am having a really difficult time understanding the logic of how a function and a list interact. Take these examples:

def list_function(x):
    return x
n = [3, 5, 7]
print list_function(n)
n = "Hello"
def string_function(s):
  return s + "world"
print string_function(n)

How do n list and function interact? I knew the answer but don’t how it works I just have the syntax memorized but I feel I need to really get what’s happening instead of memorizing. If someone could give a for dummies explanation of the logic so I can wrap my head around it I would be greatly indebted.



It works with the argument.

Take the first function. It’s passed one argument (x).
But x is currently just there to tell your function that it will be able to receive an argument.

So you have your function. It’s ready for use, it will return the value of whatever argument is given to it.
Once your list_function receives an argument, it will process its information.

But to use your function, you first need to call it.

So when you print list_function, it knows that it needs to receives an argument to return something.
In this case, list_function receives (n) and not (x) anymore.

Because (n) = [3, 5, 7], the function will return n, thus [3, 5, 7].


def list_function(x):
    return x


def list_function(n):
    return n

and so it returns the value of n, which is [3, 5, 7].

then you can call your function using other arguments.

letters = [‘A’, ‘B’, ‘C’]

print list_function(letters)


def list_function(letters)
    return letters

#will output [‘A’, ‘B’, ‘C’]

Of course, you never actually change your function. You just call it using different arguments.


That helps a lot; I was close! Thanks! Really appreciate you


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