+ 1
What is the real time use of functions in python....i.e. what's their use in making gui etc....
curious with functions...
2 odpowiedzi
+ 1
functions are to compartmentalize code. When code runs it reads on the root level. Then checks if any functions are executed and runs the function where ever its located in the code.
example. # sorry for indents writing on phone and made a mistake
if you are spooked you will drop a ball.
import time
scarelevel = 0
def bescary:
global scarelevel
scare = input()
if scare == "BOO":
scarelevel += 100
else:
scarelevel += 10
scarecheck()
def scarecheck():
global scarelevel
if scarelevel == 100:
dropball()
else:
bescary()
def dropball():
print("*drops ball* EEEEK!!")
time.sleep(5)
exit()
bescary()
if you write that without functions it only runs once and stops there. with functions you can run functions only when needed and if you'd run this with loop it would run over and over again and in cases it takes unnecessary resources
In guis you might have functions to draw input boxes and images and another to write stuff. you just add them to the __init__(self):
+ 1
Functions are for everything, in any programming language, not just python.
Without them your code would not be scalable, reusable, or clear, and will be much harder to debug.
If you use a GUI module such as pygame or TurtleWorld, you will see that you call functions from that module to do the things you want.
And when a piece of code exceeds ~30 lines (sometimes even less), you will see that compartmentalizing your code into separate functions just makes sense