17
Understanding Functions In Python
Function can simply be defined as a sequence of statements that execute in a certain order.For function to work it has to be called or invoked,by simply using the function’s name with parentheses.
There are three types of functions in Python:
1.Built-in functions : These are functions that come preloaded in python.You only need to call them.Examples: help(),print() , min()
2.User-Defined Functions (UDFs):These are functions created by the user to avoid repetition of code.In this blog,We will majorly focus on this type of functions.
3.Anonymous functions: which are also called lambda functions because they are not declared with the standard def keyword.
In python,UDFs are defined using a keyword "def"
def function_name(arguments):
write your code here
print()
Note: A function can take an argument(1 argument or more) or no argument.You can add as many arguments as you want, just separate them with a comma.Further discussion on argument will be discussed on a different section of this blog.
Example 1 : Create a function with no argument
#Function with no argument
def Greetings():
print('Hello ,Merry Christmas')
Greetings()
Example 2 :Create a function that multiply any number passed to it by 10 and takes one argument
def Multiply_By10(num):
num = num * 10
print(num)
#calling the function
Multiply_By10(7)
Result: 70
For more examples on arguments , check out this Notebook.
Before proceeding, you will realize , in some context ,they will use the name Argument and parameters interchangeably.
Both are used in the function, but a parameter may refer to a variable listed inside the parentheses in the function definition . In Example 2 , "num" is a parameter.
Now, an argument may be defined as the value that is sent to the function when it is called. In example 2 the value "7" is an argument
These are arguments with default values assigned to them during function definition. We assign a default value to an argument using the assignment operator in python(=).
When calling such a function without passing any argument it will use the default value. Example:
def Say_Name(user="Kim"):
print(f'Welcome back home,{user}'
Say_Name()
#This will return
Welcome back Home ,Kim
NB:For all types of arguments example codes, check out this Notebook on Google Colab
In python functions,the values passed through arguments are assigned to parameters in order, by their position.
With Keyword arguments, you have full control and flexibility on the values passed as arguments.
Example:
def multiply(a,b):
return a*b
e=multiply(b=10,a=19)
d=multiply(a=19,b=10)
print(e)
print(d)
#Result
190
190
Arbitrary arguments saves the situation where you don't know how many arguments the function will take.
When defining functions ,We place an asterisk ( * ) before the parameter to denote that the function can take an arbitrary number of arguments.
Example:Create a function that multiply numbers
def feat(*args):
mult = 1
for arg in args:
mult = mult * arg
return mult
print(feat(9,8,5,12,16))
#Result
69120
Example 4
def Funt(*args):
for arg in args:
print (arg)
Funt('Hello', 'Welcome', 'to', 'Kenya')
#Result
Hello
Welcome
to
Kenya
However, there are Arbitrary Keyword Arguments which will help in situation where you don't know how many keyword arguments that will be passed into your function.In such situation we add two asterisk ** before parameter name in the function definition.
Example
def fun_1(**kwargs):
for key,value in kwargs.items():
print("%s==%s"%(key,value))
fun_1(name='titus' ,home='Costa Rica')
You will realize that *args and **kwargs both take uncounted number of arguments. However, the two differ in the following way:
- *args- use it when iterating through a list
- **Kwargs - use it when iterating through a dictionary
Lambda function is a function that take any number of arguments, but can only have one expression.We don't assign names to lambda functions and can be used inside other functions.
Syntax
lambda arguments : expression
Example
multiply_by10 = lambda x: x *10
print(multiply_by10(69))
#Results
690
By now,you should understand how to write function and using different arguments.But wait, do you know that these arguments follow some orders ?
Yes they do,I will list their order below:
- Default Argument
- Keyword Argument
- Arbitrary Argument
- Arbitrary Keyword Argument Example:
def cup(t = 10,a,b,*args,**kwargs):
pass
THANK YOU READING.
Follow for more post
17