Python : User defined functions

Hello Friends,
Today in this article we are going to discuss about Functions in Python
What are functions?
A function is the set of statements that are organized together to perform a specific task. Function are called to perform each task sequentially from the main program.
Importance of functions
- Functions are useful when the size of program is too large or complex.
- Function improves readability of a program.
- They helps in splitting a complex task into smaller blocks and each block is called Function.
- Functions are useful when a block of statements to be written again and again
How to define functions
- Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
- Any input arguments should be placed within these parentheses
- The first statement of a function can be an optional statement – the documentation string of the function
Syntax of function –
def Factorial number(arg1, arg2,...):
Function with arguments
def add(a, b):
return a + b
num_sum = add(a = 1, b = 2)
print(num_sum)
-------------------------------
In [1] runfile(...)
3
Function without arguments
A function without arguments can be created by empty parenthesis because all arguments inside function are passed within parenthesis
#Program to create function without arguments and without return statements
def Thanks ():
print("Hello")
print("goodbye")
#calling the function
Thanks()
---------------------------
In [1] runfile(...)
Hello
goodbye
The name of function is Thanks which is succeed with an empty parenthesis.
#Program to show for calling a function a multiple times
def Thanks():
print("Hello")
print("goodbye")
#Calling the function multiple times
Thanks()
Thanks()
Thanks()
-------------------------
In [1] runfile(...)
Hello
goodbye
Hello
goodbye
Hello
goodbye
We have very good series of Python articles, please have a look β https://knowledge-junction.in/category/python/
Thank you for reading this article π
Have a nice day π
1 Response
[…] unlike normal functions it can evaluate or return function in only in one expression. It works like def function but can take arguments in only one […]