So in this session we'll learn about how you can create and call various types of
functions in Python. So let's proceed and learn about function. So what are these
function. Well a function is a block of organized reusable set of instruction
that is used to perform some related action
but why do you even need a function? Well what if you have to write a program in
which you have to use same set of code again and again. For example you wrote a
code of 20 lines or 40 lines and those 40 line of code are coming in your main
program at 10 different places and you have to use those 40 line of code 10
different times in the program. So I don't think that none of you would be
interested in writing the same lines of code again and again. So in order to
resolve this issue they created function. So well this
function is a block where you write your code which you think you might use it
again at some point of time, therefore it allows the reusability of the code and
thus minimizes the redundancy. In Python they are basically two types of function
the first one is the user defined and the second is built in function. Let's see
them one by one so what is a user-defined function
well any function defined by user is a user defined function
syntax
it starts with a keyword def after that an identifier for a function name and
then you pass the arguments into it all right followed by semicolon and after
that you mention the set of statement inside it alright? let's see an example
for example if you want to add two arguments so you define a function as ad
and inside ad you pass two parameters as A and B so sum equal A plus B and return
some. that's it. It's as simple as let me just define a user-defined function for
you. It's like it start with the keyword def after that I I'll write a function
named as ad inside that I'll pass two arguments A and B semicolon and this
function should return me sum of A and B all right
fine so that is my user-defined function
next is the Built in function. Now these built-in function are a predefined set
of functions in Python. We have ABS function which it runs the absolute
value of a number. We have all function which returns true if all items and an
iterable objects are true. We have any function, ascii function, bin function,
bool function and many other built-in functions are available there in Python
if you want more information about it you can just check out the Python
official doc. Now that we have defined a function now
let's see how we can call a function. So calling a function, well there are two
ways of calling a function. You can either call it by passing a value or by
passing a reference.Let's see them one by one
so first is pass by value. Calling a function by passing the value for
example a equal to ten the def that is keyword so we are
defining the function as print the value of B is B and again inside the function
we are updating the value of B to be hundred and print the new value of B is
B and then what you are doing change it and we are passing A. Now since the value
of a was 10 so this function would take the value of B as 10 so print value of B
is 10 and new value of B is 100. Let me just show you in order to resolve this
issue let's define the value as a equal ten
define my function
that I'll pass an argument as B print
the value of B is
B
fine and next we'll update the value of B to be
hundred and again print
the new value of B is
B. Fine so this is my function
now what I do? I ll call my function by passing a value calling a function
by passing a value to it
all right my function and inside that I'll pass the value as A. Since the value
of a was 10, let's see the output. see, so we got the output as the value of B is
10 and the new value of B is 100 so this was about calling a function by passing
a value to it next we have is pass by reference
calling a function by passing the reference for example so we have a list
as c = 10 20 30 so we are defining a function as change them and we are
passing an argument D to it. Inside the function we have print value of D is d, D
of 0 it's a numeric constant. We are defining it as 99 D of 1 as 98 then
print the value of D is D then change them. Now when we are calling a function
by passing the reference so what we are doing we are just passing the entire
list of it so what do we get the output? will get
the output as value of d is 10 20 and 30 and the new value of d is 99 98 and 30
let me just show you one example, C equal 10 comma 20 comma 30 right? now let's
define my function 2 inside this D is what
print
the value of D is d all right? next we ll define some numeric literals like like D
of 0 is equal to 99, D of 1 equals 98 and then print
the new value of D is
again d
fine, this is my function now if I call my function using pass by reference so
my function 2 and I pass the value C into it so let's see what we get the
output so we got the output as the value of D is 10 20 30 and the new value of D
is 99 98 & 30 fine
let's move ahead
well when a parameter is passed by reference the caller and the callee uses
the same variable for the parameter well if the collie modifies the parameter
variable effect is visible to the collars variable but when a parameter is
passed by value the caller and callee have to independent variable with the
same value if the quality modifies the parameter variable the effect is not
visible to the column well the things that you need to note in
this definition are well the variable volt here means the qualit wearable
itself not as if I pass a local variable by reference and assigned to it it will
assign that Kalles variable itself you know what is the meaning of reference
and pass by reference well it is different from the general term
reference it is that this reference is temporary and implicit what the quali
basically get is a variable that is somehow the same as the original one
here on the screen is a very good example of pass by reference and pass by
value all right pass by reference you are filling a cup
using the reference of the cup and in pass by value you are actually filling
the cup using some values so this was all about how you can define a function
and how you can call it in Piketon
let's get back to the quiz and test yourself the following function takes
two parameters and print the first one so fill in the missing code but in the
missing code what is the code my definition function takes first name
last name and what that should print so post your answer in the comment section
below and get it verified with us
honey next question is a function which
returns the X parameter plus 10 all right fill in the missing code
define my function as X and write the entire function for it you have to write
a function which returned the experimental plus 10 and again post your
answer in the comment section below and will let you know if it is correct or
not
similarly we have fourth question what is the output of
fruit we have we have a cupola pure as fruit equal apple banana and cherry if
mango and fruit then trend three plus four else print two into two
again print the output of the following code in the comment section below with
the question number Holly
now let's discuss about the lambda function in Python so what is this
lambda function well a lambda function is a small anonymous function lambda
function can take any number of arguments but can only have one
expression note the fact that lambda function cannot have more than one
expression so here's the syntax
lambda argument : expression for example we have X equal lambda equal and a plus
10 and print X of 5 so what it is basically doing it's defining a lambda
function that adds 10 to the number passed in as an argument and then print
the result let me just show you an edge over a
notebook let's define a lambda function as X
equal lambda
of a such that a plus 10 and then print X inside that will pass a value of five
so what do you think the output should be 5 plus 10 it would be 15 all right if
you change 5 to 10 the output would be 20 all right
let's move ahead so next is function versus lambda function so what we are
doing up here we are comparing function with a lambda function so here's a
normal function which takes two argument and return the multiplication result of
both of them and here is a lambda function for the same it's like our
equal lambda X comma Y such that X multiplied by Y and if you call our 12
comma 3 water will do it will return the result of 12 x 3 that is 36 I'll just
show you this energy over a notebook for example I'm defining a function as
de f let's name my function as my team I'll pass two arguments up here x and y
: now return me the product of x and y that's it it's done right
now what if I have to define a lambda function for this so I'll write like Z
equal lambda
of X comma Y such that it equals to X multiplied by Y fine now if I want to
test this I will write print Z off I'll pass two values on it 12 comma 3 so it
should return me as 36 right so yeah I got the output as 36 fine let's move
ahead next is the power of lambda function
well the power of lambda function is better shown when you use them as an
anonymous function inside another function for example let's say you have
function definition that takes one argument and that argument will be
multiplied with a unknown number right for example you have my function as n
returns lambda a such that a plus n
so you're defining a variable my sum equal my function of three for example
at seinäjoki notebook
define my function
and we'll pass an argument as n such that it should return
lambda of a says that a x
and honey
now you can use this function definition to make another function that always
doubles the number you send in for example I can write double me
equal my function I want to double it so my function of
two I need to multiply it with two now print what number do I need to multiply
so double me for I won the double of 12 so double me 12 the output would be 24
so if you want to triple it just change it to 3 you are put would be 36
and so on
Holly
so this was all about the lambda function fine
it's test yourself right lambda function that sums the argument a B and C and
then print the result note post down your answer in the
comment section below alright next quizzes what is the output of
if my function and return lambda a into n my W my function to my table 11 very
simple we just discussed it so just post your answer in the comment
section below okay so this was all about the lambda function so this was all for
this session in case you have any doubt you can reach out to in telepath 24/7
and we'll try to resolve your query at the earliest thank you