I am currently learning Python and looking at how to define and use functions. But I have a question about something that I find confusing.
In the tutorial there is something like this:
alphabet = ['a','b','c']
text = input("type your message")
shift = int(input("type the number of characters to shift message by"))
def encrypt(txt_msg, shft_msg):
for letter in txt_msg:
position = alphabet.index(letter)
# rest of code...
print(result)
encrypt(text, shift)
So I understand when I call encrypt(text,shift)
I am passing those arguments to the parameters in the function txt_msg
and shft_msg
, so that they are available within the function.
So why then have they not also passed alphabet
as well? In my mind, based on experience with MATLAB, if you want variables from the main workspace to be available for the function, you have to pass them as arguments. Why is alphabet
available to the function without passing it as an argument? If that is the case, what is the point of passing any arguments?