You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
deffunction():
#Here goes everything(block of code)defsay_hi():
print('Hi')
#This function will say hisay_hi() #invoke the function
Return values from functions
It exits the function
Outputs whatever is placed after a return
Everytime there is a function call there is an instruction added to the stack
defsquare_of_two():
return2**2
Parameters in the Functions
defsquare(num):
returnnum**2# These parameters only exists in the functionsdefadd(a,b):
returna+b#Parameters should be named in a better way that makes sense
Parameters VS Arguements
Parameter is in the declaration of the function
Arguement is what we pass into the function
defmultiply(a,b):
returna*bprint(multiply(1,5))
# a & b are parameters# 1 & 5 are arguements# Order matters
Common Mistakes Made while returning in a Function
Now the function will return the sum of the odd numbers because we have idented the return keyword
Default Parameters
We can also set the default values in the functions
defsquare(num,power=2):
returnnum**power#Even if we dont pass a value for the power it will retain its default value 2
Default parameters can help you save from errors
It makes our code more flexible
Default Parameters can be any values we can also pass functions inside of a function
Passing Functions inside of function
defadd(a,b):
returna+b#The math function takes in add function as a parameter alsodefmath(a,b,fn=add):
returnadd(a,b)
Keyword Arguements
Using keyword arguements we can alter the order
The reason we use the keyword arguements is because it helps you cause less errors and makes it more explicit
defexponent(num,pow):
returnnum**pow;
print(exponent(pow=2,num=3)) #This will return 9
Scopes
There are rules where are variables can be accessed
Whenever we define a variable inside of a function it will be part of the function
x=2defadd_two(num):
returnx+num#In this example the x variable is available outside the functiondefadd_three(num):
y=3returnnum+y#In this example the y is just the part of the function and it cannot be accessed from outside
Global scope
A variable that is defined outside of the function it is global
If we want to manipulate a variable that is not defined inside of the local scope we use the keyword global.
"""This will allow us to document the function better# These are called the docstrings in python__doc__ they are used to document the functions and it helps us learn better.print.__doc__# All built in functions have a docstringdef add(x,y): """Addfunctionwilltaketwoparametersxandy. Thenaddsthem""" return x + yprint(add.__doc__)"""
** and * Operators in the Functions
star operator allow us to pass in as much parameters we can and it gathers them as a tuple
The standard keyword is **args but you call it whatever you like.
# Lets say we want to add 5 numbers we will write a function and pass 5 parametersdefadd(num1,num2,num3,num4,num5):
returnnum1+num2+num3+num4+num5print(add(1,2,3,4,5))
#But what if we want to add 3 number with the same function then we have pass default values and it gets messy and takes a long time so therefore we use *args for function.defaddition(*args):
total=0fornuminargs:
total+=numreturntotal#This is better function now#args parameter will say the input in the tuple
# **kwargs parameter this will save the arguements passed into a dictionary # **kwargs is standard keyword used in the communitydefcolor(**kwargs):
forperson,colorinkwargs.item():
print(f"{person}'s favorite color is {color}")