2

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?

  • 3
    Its because, in the mind of the writer of this code the `alphabet` variable is constant and unchanging, and so is suitable to be global. But, as a programmer, you should try very, *very*, **very** hard not to use globals. – quamrana Jun 16 '23 at 09:43
  • 2
    If there is no local variable, python looks for a one in the enclosing namespace. – user2390182 Jun 16 '23 at 09:43
  • 1
    It's a matter of choosing if a global variable is ok in this context (here alphabet will be constant, so it's probably fine to be global). You could read https://stackoverflow.com/questions/62010259/the-rule-of-thumb-as-to-when-we-should-use-a-global-variable-in-a-function – Demi-Lune Jun 16 '23 at 09:43
  • https://www.w3schools.com/python/python_scope.asp – user2390182 Jun 16 '23 at 09:43

3 Answers3

2

All the variables in your code are global. To answer your second question, there's no actual "need" to pass them. Now to answer your first question, you can pass alphabet as well, but it looks like something constant and will be reused for different inputs of encrypt. To make it more reasonable and pythonic, consider the following:

ALPHABET = ['a','b','c']

def encrypt(txt_msg, shft_msg):
    for letter in txt_msg:
        position = ALPHABET.index(letter)
        # rest of code...
    print(result)

def main():
    text = input("type your message")
    shift = int(input("type the number of characters to shift message by"))
    encrypt(text, shift)

def other_function():
    text = 'random_text'
    shift = random.randint(1, 26)
    encrypt(text, shift)

Now the ALPHABET is global because it is a constant. While text and shift are local variables (in main and other_function) so they need to be passed to encrypt.

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
  • Ok thanks for the explanation. This makes more sense now. I am surprised this wasn't explained in the course. Perhaps it will be explained later, I don't know. defining the main script as a "function" as well actually resonates with me also. I think it makes things clearer. Perhaps this will also be brought up later, but I am only at the beginner stages. – Otispunkmeyer86 Jun 16 '23 at 10:28
1

alphabet is in the global scope, its defined outside of all functions and is available in all functions.

txt_msg and shft_msg are function scope, defined as function arguments, and are only available within the function itself.

read more here

execution is from top down.

Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28
1

Alphabet is a global variable. You may use any global variable inside your function.

So Whats the point of passing arguments then?

The point of passing arguments is that you can use the values that have not been defined yet at the time you're creating the function.

The value of txt_msg and shft_msg is not known when you are defining the function. Unless you write these as arugments there is no way for you to write the function and then provide the values afterwards