KEMBAR78
Functions Programming in Python Language | PPTX
Python Functions
● Define functions
● Passing arguments to Function
● Return a value from function
● Scope of Objects
● Default arguments
● Positional and keyword arguments
● Variable length arguments
Functions
 A function is a block of code that performs
a specific task.
 Piece of reusable code
 For performing repeated, we can make a
function so that instead of writing the same
code again and again for different inputs
 Call function instead of writing code
yourself
Functions Types & Benefits
 Types of Functions in Python
◦ Built-in library function: These are
Standard functions in Python that are available
to use.
◦ User-defined function: We can create our
own functions based on our requirements.
 Benefits of Using Functions
◦ Increase Code Readability
◦ Increase Code Reusability
Built-in Functions
Mohammed Sikander 5
Syntax of Function
Defining a function
Function Call
 Once we have defined a function, we can call it from
another function, program or even the Python prompt.
 To call a function we simply type the function name
with appropriate parameters.
Calling the Function
my_function()
Square function:Take one arguments and
prints its square
Square function:Take one arguments and
returns its square
Function returning multiple value
Scope and Lifetime of variables
 Scope of a variable is the portion of a program where the variable is
recognized.
 Parameters and variables defined inside a function is not visible from
outside. Hence, they have a local scope.
 Lifetime of a variable is the period throughout which the variable exits in
the memory.The lifetime of variables inside a function is as long as the
function executes.
 They are destroyed once we return from the function. Hence, a function
does not remember the value of a variable from its previous calls.
Default Arguments
 Function arguments can have default values in Python.
 We can provide a default value to an argument by using
the assignment operator (=).
Default Arguments
 In this function, the parameter amount does
not have a default value and is required
(mandatory) during a call.
 On the other hand, the parameter
discountPercentage has a default value of 0. So,
it is optional during a call.
 If a value is provided, it will overwrite the
default value.
 Any number of arguments in a function can
have a default value.
Default Arguments
 Once we have a default argument, all the
arguments to its right must also have
default values.
 SyntaxError: non-default argument
follows default argument
Keyword Arguments
 Positional argument cannot follow
keyword argument
Variable number of arguments
Functions as Objects
● Although functions are created differently from normal
variables, functions are just like any other kind of value.
● They can be assigned and reassigned to variables, and
later referenced by those names.
Functions Programming in Python Language

Functions Programming in Python Language

  • 1.
    Python Functions ● Definefunctions ● Passing arguments to Function ● Return a value from function ● Scope of Objects ● Default arguments ● Positional and keyword arguments ● Variable length arguments
  • 2.
    Functions  A functionis a block of code that performs a specific task.  Piece of reusable code  For performing repeated, we can make a function so that instead of writing the same code again and again for different inputs  Call function instead of writing code yourself
  • 3.
    Functions Types &Benefits  Types of Functions in Python ◦ Built-in library function: These are Standard functions in Python that are available to use. ◦ User-defined function: We can create our own functions based on our requirements.  Benefits of Using Functions ◦ Increase Code Readability ◦ Increase Code Reusability
  • 4.
  • 5.
  • 6.
  • 7.
    Function Call  Oncewe have defined a function, we can call it from another function, program or even the Python prompt.  To call a function we simply type the function name with appropriate parameters. Calling the Function my_function()
  • 8.
    Square function:Take onearguments and prints its square
  • 9.
    Square function:Take onearguments and returns its square
  • 10.
  • 11.
    Scope and Lifetimeof variables  Scope of a variable is the portion of a program where the variable is recognized.  Parameters and variables defined inside a function is not visible from outside. Hence, they have a local scope.  Lifetime of a variable is the period throughout which the variable exits in the memory.The lifetime of variables inside a function is as long as the function executes.  They are destroyed once we return from the function. Hence, a function does not remember the value of a variable from its previous calls.
  • 15.
    Default Arguments  Functionarguments can have default values in Python.  We can provide a default value to an argument by using the assignment operator (=).
  • 16.
    Default Arguments  Inthis function, the parameter amount does not have a default value and is required (mandatory) during a call.  On the other hand, the parameter discountPercentage has a default value of 0. So, it is optional during a call.  If a value is provided, it will overwrite the default value.  Any number of arguments in a function can have a default value.
  • 17.
    Default Arguments  Oncewe have a default argument, all the arguments to its right must also have default values.  SyntaxError: non-default argument follows default argument
  • 18.
  • 19.
     Positional argumentcannot follow keyword argument
  • 20.
  • 21.
    Functions as Objects ●Although functions are created differently from normal variables, functions are just like any other kind of value. ● They can be assigned and reassigned to variables, and later referenced by those names.

Editor's Notes

  • #2 Function is a group of related statements that perform a specific task. Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable. Furthermore, it avoids repetition and makes code reusable.
  • #5 Keyword def marks the start of function header. A function name to uniquely identify it. Parameters (arguments) through which we pass values to a function. They are optional. A colon (:) to mark the end of function header. Optional documentation string (docstring) to describe what the function does. One or more valid python statements that make up the function body. Statements must have same indentation level. An optional return statement to return a value from the function
  • #10 import math def quadEquation(a,b,c): x1 = (-b + math.sqrt( b**2 - 4 * a * c)) / (2 * a) x2 = (-b - math.sqrt( b**2 - 4 * a * c)) / (2 * a) return x1,x2 if __name__ == "__main__": print("Program to calculate the quadratic equation ") a = int(input("Enter the value of a : ")) b = int(input("Enter the value of b : ")) c = int(input("Enter the value of c : ")) x1,x2 = quadEquation(a,b,c) print(x1, x2)
  • #11  def my_func(): x = 10 print("Value inside function:",x) x = 20 my_func() print("Value outside function:",x)
  • #14 total = 0 def add(a , b): global total total = a + b print('Inside add total = ', total) add(4, 9) print('Main Block total = ', total)
  • #15 def calculateTotal(amount , discountPercentage = 0): discountAmount = discountPercentage / 100 * amount return amount - discountAmount amount = 500 totalBillAmount = calculateTotal(amount , 10) print(totalBillAmount) amount = 500 totalBillAmount = calculateTotal(amount) print(totalBillAmount) 450 500
  • #20 def mysum(*a): total = 0 for ele in a: print(total,"+",ele,"=", end='') total += ele print(total) return total print(mysum(5, 2, 9, 4)) ------------------------------------- Built-in sum function works with list (not variable number of arguments) Max function works with variable number of arguments --------------------------------------------------------------- def mymax(a,*b): large = a for ele in b: if ele > large: large = ele return large print(mymax(4,5,7,3,1,8,2))
  • #21 def multiply(x, y): return x * y a = 4 b = 7 operation = multiply print(operation(a, b))
  • #22 def add(a,b): print("Add") return a + b def sub(a,b): print("Sub") return a - b def mul(a,b): print("Multiply") return a * b def div(a,b): print("Divide") return a / b if __name__ == "__main__": print("Arithmetic Operations") print("Enter two numbers ") a = int(input()) b = int(input()) print("1. Add 2. Sub 3. Multiply 4. Divide") ch = int(input()) calculate=add if ch==1 else sub if ch==2 else mul if ch==3 else div res = calculate(a, b) print(res)