FUNTIONS By Siddique Ibrahim APCSE Why function It
FUNTIONS By Siddique Ibrahim AP/CSE
Why function? ? • It is very difficult to maintain large- scale program. • Very complex to identification of the flow. • Hard to understand by other user/programmer.
Solution!!! • Big problem can be divided into small modules and repeatedly call these modules whenever there is a need.
How to divide the program? • With help of functions: • Improve the code’s readability • Flow of execution.
Definition • Are blocks of organized, reusable code used to perform single or related set of actions. • • Provide better modularity and high degree of reusability • Python supports: - Built-in functions like print() and - User - defined functions
Function (contd. , ) • Defining a function: • Function is a self contained blocks of one or more statements theat performs a specific task when called
Syntax Function Header • def name_of_function( Parameters): • Statement 1 • Statement 2 • Statement 3 • …………. • Statement N • return [expression] • Function Body
• In python function starts with a keyword ‘def’ followed by function_name, parenthesis (()) and a colon : • Funtion definition starts with def keyword followed by name of the function • One or more arguments(separate by commas) are placed inside these parenthesis. These parameters are called the formal parameters. • Function block can have optional statement/comment • Every line inside code block is indented • return statement with no expression is same as return None
Simple Example 1 • def display(): • Print(“Hi all!!! Welcome to python function”) • display() # function call • Note: function definition should be coded first followed by function call
Example 2 • def print_mes(): # Called Function • str 1=input(“Enter your name”) • Print(“Dear “, str 1, ” Welcome to python programming”) • Print_mes() • # Function call
Exercise No: 1 • Get the electronic shop offers using function and print the same
Different types of formal arguments 1. Positional Arguments 2. Keyword Arguments 3. Parameter with default values 4. Variable length Arguments
1. Positional Arguments • If there are more than one parameters, how does Python know which argument in the call statement has to be assigned to which parameter in function definition?
Answer…. • The parameters are assigned by default according to their position. • The first argument in the call statement is assigned to the first parameter in the function definition. • Similarly, the second argument in the call statement is assigned to the second parameter in the function definition and so on.
Example Program • def fun_type 1(Name, roll_no): #function definition • print(“Name=“, Name, ”Roll_no”, roll_no) • Fun_type 1(“XYZ”, 100) # funtion call • Fun_type 1(200, ”ZYX”) # funtion call • The above program the first argument in the call statement binds to the first parameter
Predict what will be the output • def sampleprint(Name, no): print ('Name=', Name, 'No=', no) • sampleprint('18 bce 000') •
Output
2. Keyword Arguments • Alternative to positional argument • Programmer can explicitly use the parameter name while calling the function. • Programmer can pass a keyword argument to a function definition by using its corresponding parameter name rather than its position. • Syntax: • Name_of_funtion(keyword 1=value 1, keyword 2=va lue 2…. )
Sample Program
Output
• The above program, the statement fun_type 1(roll_no=100, Name=‘Sample’) passes the value 100 to parameter ‘roll_no’ and ‘sample’ to the parameter ‘Name’. • It means arguments can appear in any order using keyword arguments. •
Precautions • def display(num 1, num 2): print("Number 1=", num 1, "Number 2=", num 2) • display(40, num 2=20) • • Output: • >>> • RESTART: E: /Siddique/Python programming/Program/function_position_precaution 1. py • Number 1= 40 Number 2= 20 • >>>
But he/she cannot invoke the function • display(num 2=20, 40)
2. Programmer cannot duplicate an argument
Python doesn’t support multiple values for parameter num 1
3. Parameter with default values • Parameters within a function definition can have default values. We can provide default value to a parameter by using the assignment(=) operator.
Two values passing in function call
Output? ? ?
Output
4. Variable length arguments
Local and Global scope of a Variable • Local Scope: • Variable that are initialized within a function • Global Scope: • Variable that are initialized outside the functions are said to exist in global scope.
Example Program
Local Variable cannot be accessed outside the function(Global Scope)
Global variable read from local scope • s="I Love Python" • def example(): print(s) • example() •
Local and Global variables with the same name • s="I Love Python" • def example(): • s="I too coding in C" • print(s) • example() • print(s)
The Global statement • Suppose if the programmer wants to change the value of global variable within a function. • Use global keyword.
Program without using global statement
Program using global statement
Return Statement • Return a value from the function • Also used to return from a function #Break out of the function
Terminating function using return
Recursive Function • Call the function itself. • Example factorial program: def rec_fact(x): if x==1: return 1 else: return(x*rec_fact(x-1)) num=5 print("The Factorial of", num, "is", rec_fact(num))
Return Multiple Values in function def fun(): str="This is the Python program returning multiple values" x=200 return str, x=fun() print(str) print(x)
Lambda Function • Anonymous function • Not bound to a name • Only have a code to execute that which is associated with them. • Does not contain return function • It contain single expression as a body and not a block of statements as a body. • Syntax: • Name= lambda(variables): code
Without Lambda function • def fun(x): return x*x*x • print(fun(3)) •
With Lambda function • cube=lambda x: x*x*x • print(cube(3)) • Lambda x: x*x*x creates a lambda() function called cube, which takes a single argument and returns the cube of a number.
Exercise No 2 • What will be the output of the following program?
- Slides: 54