Being relatively new to Python in this question I may use some incorrect terminology and will display misunderstanding - which I why I am here.
I am studying Python functions and am trying to ensure I understand how variables are passed and returned.
I have written this trivial function to sort items in a list
def func_sort_list(NN):
for w in NN:
print (w, type(w))
NN.sort()
print (NN)
return (w)
I have assigned values to a list
unsort_letters=['q','w','e','r','t','y']
Then I envoke the function
func_sort_list(unsort_letters)
and get the result
q <class 'str'>
w <class 'str'>
e <class 'str'>
r <class 'str'>
t <class 'str'>
y <class 'str'>
['e', 'q', 'r', 't', 'w', 'y']
'y'
then, after execution, if I display the contents of the variable I originally passed to the function I get
unsort_letters
['e', 'q', 'r', 't', 'w', 'y']
Which is the result I want.
Is the following interpretation of what happened correct so I can feel a bit more secure when writing functions?
The original value of unsort_letters , ['q','w','e', ...], is "global"?
By calling func_sort_list(unsort_letters) I have passed the address / pointer of unsort_letters to func_sort_list?
NN is a variable "local" to the function but it contains the pointer/address to the passed variable and since a list is mutable the contents of unsort_letters is being changed within the function?
Which leads me to:
Is there ever a circumstance when I cannot change the contents of the passed parameter within the function and I have to write something like the following?
def func_return_only(input_parm): result_var = << some processing on input_parm goes here>> return (result_var)
Which I have to envoke as follows to get at the value of the returned values in var_s.
var_s = func_return_only(<< my input variable or value>>)
?