[General] Different parameters


#1

It’s a general question. I was just doing Python exercises were I had to write a simple function. I got the function right, and understand how it works etc, except that my brain freezed how the function can take in a different argument than the parameter you give in the function definition. I pasted a code for example.

How can it be that the function uses ‘n’ (I know, that in the end I call the argument ‘n’), but we defined the function with ‘s’. Is the parameter ‘s’ just a placeholder or?

Thanks in advance!

n = "Hello"
# Your function here!
def string_function(s):
  return str(s) + "world"


print string_function(n)

#2

yes, s a placeholder and this is possible given functions only execute when called

for example:



n = "Hello"
# Your function here!
def string_function(s):
  return str(s) + "world"


print string_function(n)
print string_function("waynes")

is perfectly possible, the second function call s will get passed a different value then the first function call


#3

Ah oke, it makes sense now!

Thanks for the fast reply


#4

Your function receives values, not variables


#5

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