Function in Python
A
function is a set of codes which executes at a time and used to perform some
actions
Function
Syntax
Def:
Key
Function
Name
Input
Values
Function
Body
Return
Value
Key Name
Input Body ReturnValue
"def" funcname "(" [parameter_list]
")" ":" suite
Function Code snippet
#Function without input/output
Definition
def my_function():
print("Hello
Function!")
Calling
my_function()
#Function with input
def
printhello( str ): "This is hello
test print function"
print str
return;
printhello(“hello”)
# Function with return
def sum_two_numbers(a, b):
return a + b
sum_two_numbers(5,7)
#
Function with input/output
def printhello( a, b ):
return a + b;
Recursive functions
def
factorial(n):
if n == 1:
return 1
else:
return n * factorial(n-1)
factorial(5)
Function Arguments
Required
arguments
Keyword
arguments
Default
arguments
Variable-length
arguments
Function in Python
Reviewed by Rupesh
on
21:50
Rating:
No comments: