Functions
Functions
References
• Taming Python by Programming, Dr Jeeva Jose Rincy T A
• Introduction to computing and problem solving using
python by Balaguruswamy Asst Professor in Computer Science
Prajyoti Niketan College, Pudukad
2
Functions in Python
3
Functions in Python
4
Functions in Python
5
Functions in Python
6
reference vs by reference.
Functions in Python
7
def cal(a,b):
print(a+b)
return
def cal(a,b):
return(a+b)
>>>
9
>>>def print_lines():
print(“Hello Python”)
print(“Welcome to Python Programming”)
Examples
>>>print_lines()
Hello Python
Welcome to Python Programming
Functions in Python
10
▪ The rules for defining a function name are same as those for variable
names: alphabets, numerals and some special characters are allowed.
More to ▪ Giving the same name to a variable and a function should be avoided.
▪ The parenthesis after the function name contain the parameters or
read... arguments. They are optional.
▪ The first line in the definition of a function is known as header and the
rest is abbreviated as the body.
Functions in Python
11
>>>def print_lines():
print(“Hello Python”)
print(“Welcome to Python Programming”)
>>>def new_print():
A function print_lines()
calling print_lines()
another
function >>new_print()
Hello Python
Welcome to Python Programming
Hello Python
Welcome to Python Programming
Functions in Python
12
Functions in Python
13
Functions in Python
14
Functions in Python
15
# Function definition is here
def printme( str ):
"This prints a passed string into this
function"
print (str)
return;
Functions in Python
17
Functions in Python
18
# Function definition is here
return
Functions in Python
19
Functions in Python
20
# Function definition is here
def printinfo( name, age = 35 ):
"This prints a passed info into this function"
print("Name: ", name)
print("Age ", age)
return
Variable-length are not named in the function definition, unlike required and
default arguments.
arguments def functionname([formal_args,] *var_args_tuple ):
"function_docstring"
function_suite
return [expression]
Functions in Python
# Function definition is here 22
Thank You
Functions in Python