Hello everyone, I hope you are having a great day today. In today’s python blog I am going to be going over how you can define a function and how you can use that for logical grouping. I am going to start with defining a function with local scope, then I will go over defining a function with global scope, following that I will go over defining a function with local scope but with multiple inputs, and lastly, I will cover defining a function with local scope, multiple functions, and logical grouping.

For these first few examples we are going to write out a simple call and response greeting. The first we will use looks like this.

Now, to define a function you need to write “def” then the name of the function, in this case: “greeting”, and what it is looking for as it’s scope, in this case “name”. When you run this it will start at “enter_name = input(‘Enter your name:\n’). Then it will go down to “greeting(enter_name)”. From here it goes to the top where you defined the “greeting” function and it will pass in the “enter_name” as the value for “name”. Then the terminal will print “Hello (the name you entered) I hope you are doing well.” My results looked like this.

You may be asking: “Well what makes it have local scope?” The answer to that is simple. When a function is defined with a local scope, the variable that the function looks for is inside of the defined function. So, in this case, “def greeting(name)”, “name” existing within the definition is what makes the function have local scope. Now for global scope functions, the variable needs to be located within the main body of your script.

Our next call and response greeting looks like this.

This is what the same exact function would look like except using global scope. You’ll notice that greeting both in the definition and the call for it at the bottom have no variable inside of it. If we follow along like before we’ll see how this happens. The code starts with “name = input(‘Enter your name:\n’)” Then because there isn’t a specified value being referenced by the “greeting” function at the bottom, it is used to send us back to the definition of the “greeting” function. At this point we now have the “name” variable stored in this run of the code which is then used in the print statement using the “name” variable that was passed in at the beginning of the run.

The results for this look identical.

Next up, we are going to go over what it looks like when there are multiple inputs while the function has a local scope. This call and response now will return two greetings. This is what my script looks like.

When running this script we start with “name1 = input(‘Enter your name:\n’)”. After you enter your name then the script will go from “greeting(name1)” which uses the name you just entered and passes the value in from “name1” for “name” in the definition of the function “greeting”. It then prints “Hello (Your name)”. Next we move back down to the main part of the script and it looks at “name2 = input(‘Enter another name:\n’)” and then passes in the value from “greeting(name2)” for “name” in the definition of “greeting”.

My results looked like this.

Now moving on to the last one, I am switching it up to have the script run some addition instead of a greeting. My script looks like this.

The flow of this is pretty similar to the rest with some minor changes. We start in the main block, outlined by “main()”, with “num1 = float(input(‘Enter your first number:\n’))” then “num2 = float(input(‘Enter your second number:\n’))”. After these variables are set, we get “result = addition(num1, num2)” which then does 2 things. One, it swaps the values of “num1” and “num2” for “a” and “b” in the definition of the “addition” function respectively. Two, it prints “The result is (whatever the result of num1(a) and num2(b) is).” And then “main()” closes. So, on a macro level scale.“main()” opens setting the in-laid as the value for “main”, code within “main()” references and helps define “addition(a,b)”, then “main()” runs the set value for the definition of “main()”. Now, the logical grouping aspect is the “main()” block. We wrote the code like this so that it could be referenced easier in case of a need to re-use certain parts of your script. It also just helps with organization of your script since you can collapse that section of the code so it would look like this.

I hope that this last one wasn’t too daunting for you. The explanation of it makes it sound much more complicated than it is. The results of this script look like this.

And that’s where I wrap up this blog. I hope you learned something about how you can define functions and use that for logical grouping. If you have any questions feel free to reach out to me. Thank you for reading my blog and have a great rest of your day.

Bailey McDonald
Data Engineer, Patriot Consulting
Email: bkmcdonald@patriotconsultingcorp.com | Blogs: Patriot Consulting Blogs
LinkedIn: Personal: BaileyMcDonald | Company: Patriot Consulting

Leave a Reply