USER DEFINED FUNCTION
IN PYTHON
MALLA REDDY ENGINEERING COLLEGE
          Submitted To:
          Submitted By:
          Gujjula Divya
           22J41A0521
 Computer Science and Engineering
INTRODUCTION:
      A function is a block of program statements which can be used repetitively in a
program. It saves the time of a developer. There are some built-in functions which part of
python.
TYPES OF FUNCTIONS IN PYTHON:
   ➢ Built-in functions:
       Python provides a set of built-in functions like ‘print ()’, ’len()’ , ‘type’ , etc., which are
       available for use without any additional effort.
   ➢ User-defined functions:
       user-defined functions are those that users create to perform specific tasks. These
       functions are defined using the ‘def’ keyword followed by the function name and
       parentheses ‘()’.
CREATING USER-DEFINED FUNCTIONS:
        Syntax:
def function_name(parameters):
  """docstring"""
  statement(s)
EXAMPLE:
def greet(name):
  return f"Hello, {name}!"
PARAMETERS AND ARGUMENTS:
Functions can accept parameters to make them more flexible.
RETURN STATEMENT:
Functions can return values using the `return` statement.
EXAMPLES OF USER-DEFINED FUNCTIONS:
   ➢ Simple Function with No Parameters:
      def greet ():
      return "Hello, World!"
    ➢ Function with Parameters:
       def greet(name):
       return f"Hello, {name}!"
    ➢ Function with a Return Value:
      def add(x, y):
      return x + y
    ➢ Function with Default Parameter Values:
          def greet(name="World"):
          return f"Hello, {name}!"
    ➢ Function with Variable-Length Arguments:
          def sum_all(*args):
          return sum(args)
ADVANCED TOPICS:
    ➢ Recursive Functions:
Functions that call themselves are known as recursive functions.
def factorial(n):
  if n == 0 or n == 1:
    return 1
  else:
    return n * factorial(n-1)
    ➢ Lambda Functions:
Lambda functions are small anonymous functions.
square = lambda x: x ** 2
EXAMPLE: CALCULATOR APPLICATION
It implements a basic calculator with functions for addition, subtraction, multiplication,
division, power calculation, and factorial calculation.
    ➢ Function to add two numbers
        def add (num1, num2):
        return num1 + num2
    ➢ Function to subtract two numbers
        def subtract (num1, num2):
        return num1 - num2
    ➢ Function to multiply two numbers
        def multiply (num1, num2):
        return num1 * num2
    ➢ Function to divide two numbers
        def divide (num1, num2):
        return num1 / num2
print ("Please select operation -\n" \
     "1. Add\n" \
     "2. Subtract\n" \
     "3. Multiply\n" \
     "4. Divide\n")
    ➢ Take input from the user
select = int (input ("Select operations form 1, 2, 3, 4:"))
number_1 = int (input ("Enter first number: "))
number_2 = int (input ("Enter second number: "))
if select == 1:
  print (number_1, "+", number_2, "=",
            add (number_1, number_2))
elif select == 2:
  print (number_1, "-", number_2, "=",
            subtract (number_1, number_2))
elif select == 3:
  print (number_1, "*", number_2, "=",
              multiply (number_1, number_2))
elif select == 4:
  print (number_1, "/", number_2, "=",
              divide (number_1, number_2))
else:
  print ("Invalid input")
Output:
Please select operation -
1. Add
2. Subtract
3. Multiply
4. Divide
Select operations form 1, 2, 3, 4: 1
Enter first number: 15
Enter second number: 14
15 + 14 = 29
CONCLUSION:
User-defined functions are crucial for writing efficient and modular code. They help in
breaking down complex problems into smaller, manageable tasks.