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

  1. Functions are useful when the size of program is too large or complex.
  2. Function improves readability of a program.
  3. They helps in splitting a complex task into smaller blocks and each block is called Function.
  4. Functions are useful when a block of statements to be written again and again

How to define functions

  1. Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).
  2. Any input arguments should be placed within these parentheses
  3. 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 πŸ™‚

You may also like...

1 Response

  1. December 17, 2022

    […] 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 […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: