Course material: Python
Function: You can define functions to provide the required functionality. Here are simple rules to define a
function in [Link] blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).Any input parameters or arguments should be placed within these parentheses. You can
also define parameters inside these [Link] first statement of a function can be an optional
statement - the documentation string of the function or docstring. The code block within every function
starts with a colon (:) and is [Link] statement return [expression] exits a function, optionally passing
back an expression to the caller. A return statement with no arguments is the same as return None.
For Example:
def p(str):
"This prints a passed string into this function"
print (str)
return;
p("this is first calling function ")
p("This is second time calling function")
Pass by value: All parameters (arguments) in the Python language are passed by reference. It means if you
change what a parameter refers to within a function, the change also reflects back in the calling function.
For example:
def changeme( mylist ):
"This changes a passed list into this function"
[Link]([1,2,3,4]);
print ("Values inside the function: ", mylist)
return
# Now you can call changeme function
mylist = [10,20,30];
changeme( mylist );
print ("Values outside the function: ", mylist)
For Example: Note(In this program use the function but value overwrite in list).
def changeme( mylist ):
"This changes a passed list into this function"
mylist = [1,2,3,4]; # This would assig new reference in mylist
print ("Values inside the function: ", mylist)
return
CCIT- College of Computer and Information Technology Page1 of 3
Course material: Python
# Now you can call changeme function
mylist = [10,20,30];
changeme( mylist );
print ("Values outside the function: ", mylist)
Number of Arguments: By default, a function must be called with the correct number of arguments.
Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not
more, and not less.
For Example:
def fun(name, rollno):
print("This is function")
print(name + "-" + rollno)
fun("Pyhton", "310")
Arbitrary Arguments, *args: If you do not know how many arguments that will be passed into your
function, add a * before the parameter name in the function [Link] way the function will receive a
tuple of arguments, and can access the items accordingly.
For Example:
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 [Link] way the order of the
arguments does not matter.
For Example:
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3 = "Linus")
Passing a List as an Argument :You can send any data types of argument to a function (string, number, list,
dictionary etc.), and it will be treated as the same data type inside the function.E.g. if you send a List as an
argument, it will still be a List when it reaches the function.
For Example:
def my_function(food):
CCIT- College of Computer and Information Technology Page2 of 3
Course material: Python
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
Return Values : To let a function return a value, use the return statement.
For Example:
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
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
For Example:
x = lambda a,b: a + b + 10
print(x(5,6))
CCIT- College of Computer and Information Technology Page3 of 3