Can someone explain please?


#1

Take a look at this code please:

n = “Hello”

def string_function(s):
return s + ‘world’

print string_function(n)

Can someone tell me why return s + ‘world’ prints out ‘Helloworld’ when the variable for Hello is n.

Should it not be 'return n + world. either way if i use s or n, the same thing prints in console.

thanks

H


#2

s is the formal parameter and as such is a local variable. When n is passed as the argument in the function call, its value is exposed in the scope of the function, and referenced by the local variable.

    def string_function(s):
        print (s)    # Hello
        return s + ‘world’

#3

So effectively, when using print_function(n) because the variable (n) is the argument it applies the function to that variable.

phew :stuck_out_tongue:


#4

to that value. The variable is of no consequence.


#5

roger! So the variable is simply a reference to a value, be it an int, str, etc…

Thanks a lot for the help man.


#7