xxxxxxxxxx
a = 34
b = 78
def total():
print("The total of a+b is:",a+b)
#how to print def function
total()
xxxxxxxxxx
# In here im giving examples on how the def function works in python.
# Each example is separated by ===
def functionName():
# What to make the function do
==================================================================
def myfunction():
print("Hello World")
myfunction()
==================================================================
def subtractNum():
print(34 - 4)
subtractNum()
# Output: 30
==================================================================
def functionName(arg1, arg2):
# What to do with function
functionName(valueForArg1, valueForArg2)
==================================================================
def addNum(num1, num2):
print(num1 + num2)
addNum(2, 4)
# Output: 6
==================================================================
def multiplyNum(num1):
return num1 * 8
result = multiplyNum(8)
print(result)
# Output: 64
==================================================================
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
# 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
# declare a function
def function_name(param) :
# content inside a function
function_name(param) # calling function
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
xxxxxxxxxx
def name():#name of the def(function);
print("Hallo, World!")#Output is Hallo, World!;
name()#Name of the def, the programm will jump to the def;
#output:
#Hallo, World!