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
def example(): #This defines it
print("Example.") #This is the defined commands
example() #And this is the commands being run
xxxxxxxxxx
#Letters, Nums,
#& underscores
#only, can't =7
#start w/ num | _ 5 is the default value if no
# | | | value is passed
# v v v
def funcName(param1, param2=5): #The colon terminates func header
str(param1) #Typecasting is temparary
strVar = str(param1) # To make typecast perma, assign to var
param1 = param2 # 5 is passed to param1
return param1 # 5 is returned
# ^ ^
# | |_ return value is optional, if no return value, func
#Optional will end returning to func call
#return
x = funcName(7)
print(x) # prints 5
xxxxxxxxxx
def test_function(argument1,argument2,argument3) :
# Do something with the code, and the arguments.
print(argument1)
print(argument2)
print(argument3)
# Calling the function.
test_function('Hello','World','!')
# Output
'''
Hello
World
!
'''
xxxxxxxxxx
# basic function
def funcOne():
print("Hello Wordl")
funcOne()
# function with params
def funcTwo(x, y):
print(x + y)
funcTwo(2, 1)
# function with returning value
def funcThree():
return "Max Cavalera"
print(funcThree())
# function with default params
def funcFour(v="Jamal Cavalera"):
return v
print(funcFour())
# function with args as tuple
def funcFive(*args):
return args
five = funcFive(1, 2, 3, 4, 5)
print(five)
# function with dictionary
def funcSix(name, age):
return "My name is " + name + " and my age is " + str(age)
six = funcSix(name="jamal", age=20)
print(six)
# function with args as dictionary
def funcSeven(**args):
return args
seven = funcSeven(name="jamal", age=20, hobby="programming")
print(seven)
# function ignore params
def funcEight(n, /):
print(n)
funcEight(10)
# function params only args dictionary
def funcNeen(*, n):
print(n)
funcNeen(n=21)
# function recursive with params
def funcTeen(n):
if n == 0:
return 0
else:
res = funcTeen(n - 1)
print(res)
return n
funcTeen(10)
xxxxxxxxxx
# Functions with more than one input
def function_name(parameter1, parameter2):
print(f"This is {parameter1}")
print(f"This is {parameter2}")
#These arguments are called positional argument since the code will check the order of the parameters
function_name("Argument1", "Argument2")
# Keyword argument to check the position add the parameter name to the argument so the order will not matter )
function_name(parameter2="Argument2", parameter1="Argument1")
xxxxxxxxxx
#to create a function in python, do the following
#create func
def func(): #can add variables inside the brackets
print("This is the function of the func")
# to call a function, do the following
func()
#you would get a line of code saying "This is the function of the func"
# By Codexel
xxxxxxxxxx
#Function Tutoral:
def hello():
print("hello")
"""To make a function, it needs def then nameOfFunction() and a : to
make the function work, you don't need a closing tag, as long as there is
tabbed section."""
def add(a, b): #This time, there is two inputs for the function to prossess.
c = a + b
return c
"""What the function above does is you input 2 numbers, and then it returns
#The Value c, Calling it is as simple as add(5, 1)
#What return does, is it almost makes a varible. So you can do:
#70 + add(10, 20) and it will return with: 100. This is because it will
go 70 + 30, as the function returned 30 because the inputs were 10 and 20."""
"""Functions can be called by code, as long as the function has already
been defined. Hope this helped you in your python journey!"""
xxxxxxxxxx
def funcName(par1, par2=default, *args, **kwargs): # function with par1, par2
# *args takes all remaining unnamed arguments as tuple
# **kwargs takes all all remaining named arguments as dictionary
global outerVar # include variable from global scope to change it
def innerFuncName(): # define inner function
# code block
# code block
return returnValues