xxxxxxxxxx
OK, basically def function() is a block where you will have one task that you can repeat all over again in the code to make it look short and clean
xxxxxxxxxx
def example(): #This defines it
print("Example.") #This is the defined commands
example() #And this is the commands being run
xxxxxxxxxx
#plz suscribe my youtube channel --> https://www.youtube.com/channel/UC-sfqidn2fKZslHWnm5qe-A
# Parameter of the function
# |
def greetings(Name):
#Content inside the function
print("Hello",Name)
print("How are you",Name)
print(greetings("Python devloper"))
# ^
# |
# Argument of the function
xxxxxxxxxx
a = 34
b = 78
def total():
print("The total of a+b is:",a+b)
#how to print def function
total()
xxxxxxxxxx
# declare a function
def function_name(param) :
# content inside a function
function_name(param) # calling function
xxxxxxxxxx
#A function is a block of code which only runs when it is called.
#You can pass data, known as parameters, into a function.
#following is a simple function
def exmple_of_function():
print("Hello from a function")
example_of_function()
xxxxxxxxxx
def main():
#this will define your function, which you can use in the program
#or you can use it in other programs too, which ill show later
#also you have to call your function once atleast
userinput = input("String: ")
#ask the user for input, which is being stored in userinput
if len(userinput) < 5:
print("it is longer")
else:
print("it is shorter")
main()
#this will call the function
'''
to use your function in other programs, you can just do the following
from (the name of the program you want to import the function from) import (function name)
done!
Really hoped this helped
'''
xxxxxxxxxx
def function1(): # outer function
print ("Hello from outer function")
def function2(): # inner function
print ("Hello from inner function")
function2()
function1()