Python Functions
A function is a group of related statements or blocks of codes that perform a specific task.
In python, we create a function using the “def” keyword. At the end of function use colon(:) mark and write statements inside it.
return(optional) statement use return value.
>>> def name(name):
print(“hi”+ name +”kumar”)
Calling function
Calling function written using function name with parenthesis.
>>> def name(name):
print(“hi”+ name +”kumar”)
name()
Calling function using parameters
>>> def name(name):
print(“hi”+ name +”kumar”)
name("naveen")
Calling function using parameters with default value
>>> def name(name = "naveen"):
print(“hi”+ name +”kumar”)
name()
How return value using function
To return value in function return keywork is used.
>>> def value(a):
return a
value(5)
Way to send argument value
By key as argument
>>> def using_key(name3,name2,name1):
print("my friend name is " + name3)
using_key(name1 = "naveen", name2 = "sachin", name3 = "jitendra")
By arbitrary argument
When need pass multiple arguments in function then we define the argument name with (*)
>>> def using_key(*name):
print("my friend name is " + name3)
using_key("naveen", "sachin", "jitendra")
Recursive function
Python supports recursive function when need to execute value recursively.