0% found this document useful (0 votes)
21 views12 pages

Function Indo

Uploaded by

deepgyan780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views12 pages

Function Indo

Uploaded by

deepgyan780
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Function

Python Functions

 A function is a block of code which only runs when it is called.


 You can pass data, known as parameters, into a function.
 A function can return data as a result.
Creating a Function

 In Python a function is defined using the def keyword:


def my_function():
print("Hello from a function")
Calling a Function

 To call a function, use the function name followed by parenthesis:


 def my_function():
print("Hello from a function")

my_function()
Arguments

 Information can be passed into functions as arguments.


 Arguments are specified after the function name, inside the parentheses. You can add as
many arguments as you want, just separate them with a comma.
 The following example has a function with one argument (fname). When the function is
called, we pass along a first name, which is used inside the function to print the full name:
 def my_function(fname):
print(fname + " Refsnes")

my_function("Emil")
my_function("Tobias")
my_function("Linus")
Arbitrary Arguments, *args

 If the number of arguments is unknown, add a * before


the parameter name
 def my_function(*kids):
print("The youngest child is " + kids[2])

my_function("Emil", "Tobias", "Linus")


Keyword Arguments

 You can also send arguments with the key = value syntax.
 This way the order of the arguments does not matter.
 def my_function(child3, child2, child1):
print("The youngest child is " + child3)

my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")


:

Return Values

 To let a function return a value, use the return statement


 def my_function(x):
return 5 * x

print(my_function(3))
print(my_function(5))
print(my_function(9)) 1515
25
45
The pass Statement

 function definitions cannot be empty, but if you for some


reason have a function definition with no content, put in
the pass statement to avoid getting an error.
 def myfunction():
pass
Python Lambda

 A lambda function is a small anonymous function.


 A lambda function can take any number of arguments, but can only have one expression.
 Syntax-
lambda arguments : expression
 The expression is executed and the result is returned:

 Add 10 to argument a, and return the result:


x = lambda a : a + 10
print(x(5))
 Lambda functions can take any number of arguments:

 Multiply argument a with argument b and return the


result:
x = lambda a, b : a * b
print(x(5, 6))

You might also like