Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Functions

The document discusses user-defined functions in Python, explaining that functions allow reusable blocks of code and improve readability. It covers defining functions using the def keyword, passing arguments to functions including positional, keyword, default and variable length arguments, and returning values from functions. Examples are provided to illustrate different types of functions and how to define, call and pass arguments to functions in Python.

Uploaded by

dakshparashar973
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Functions

The document discusses user-defined functions in Python, explaining that functions allow reusable blocks of code and improve readability. It covers defining functions using the def keyword, passing arguments to functions including positional, keyword, default and variable length arguments, and returning values from functions. Examples are provided to illustrate different types of functions and how to define, call and pass arguments to functions in Python.

Uploaded by

dakshparashar973
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Function introduction:

----------------------
If a group of statements is repeatedly required then it is not recommended to write
these statements everytime seperately.We have to define these statements as a
single unit and we can call that unit any number of times based on our requirement
without rewriting. This unit is called function.The main advantage of functions is
code Reusability.
Note: In other languages functions are known as methods,procedures,subroutines etc

Python supports 2 types of functions:


i) Built in Functions
ii) User Defined Functions

i) Built in Functions: The functions which are coming along with Python software
automatically, are called built in functions or pre defined functions.
Ex: id(), type(), input(), eval() etc..

ii) User Defined Functions: The functions which are developed by programmer
explicitly according to business requirements, are called user defined functions.
Syntax to Create User defined Functions:
def function_name(parameters) :
----
-----
return value

Note: While creating functions we will use 2 keywords:


i) def---->mandatory
ii) return---->optional

Q) Write a function to print Hello

parameters: are the inputs to the function. If a function contains arguments, then
at the time of calling, compulsory we should provide values, otherwise we will get
error.

Q) Write a function to take name of the student as input and print wish message by
name.
Q) Write a function to take number as input and print its square value.

return Statement: Function can take input values as parameters and executes
business logic, and returns output to the caller with return statement.

Q) Write a Function to accept 2 Numbers as Input and return Sum

Note:If we are not writing return statement then default return value is None.
1) def f1():
2) print("Hello")
3) f1()
4) print(f1())

Q) Write a Function to check whether the given Number is Even OR Odd?


Q) Write a Function to find Factorial of given Number?

Note: Returning Multiple Values from a Function: In other languages like C,C++ and
Java, function can return atmost one value. But in Python, a function can return
any number of values.

Types of Arguments:
def f1(a,b):
------
------
f1(10,20)

Note: a, b are formal arguments where as 10,20 are actual arguments.

There are 4 types are actual arguments are allowed in Python.


i) Positional Arguments
ii) Keyword Arguments
iii) Default Arguments
iv) Variable Length Arguments

Positional Arguments: These are the arguments passed to function in correct


positional order.
def sub(a, b):
print(a-b)
sub(100, 200)
sub(200, 100)
i) The number of arguments and position of arguments must be matched. If we change
the order then result may be changed.
ii) If we change the number of arguments then we will get error.

Keyword Arguments: We can pass argument values by keyword i.e by parameter name.
1) def wish(name,msg):
2) print("Hello",name,msg)
3) wish(name='Ram',msg="Good Morning")
4) wish(msg="Good Morning",name="Krishna")

Note: Here the order of arguments is not important but number of arguments must be
matched.

Note: We can use both positional and keyword arguments simultaneously. But first we
have to take positional arguments and then keyword arguments,otherwise we will get
syntaxerror.
1) def wish(name,msg):
2) print("Hello",name,msg)
3) wish('Ram',"Good Morning") #Valid
4) wish('Ram',msg="Good Morning") # Valid
5) wish(name='Ram',"Good Morning") # Invalid
#SyntaxError: positional argument follows keyword argument

Default Arguments: Sometimes we can provide default values for our positional
arguments.
1) def wish(name="Guest"):
2) print("Hello",name,"Good Morning")

3) wish("Python")
4) wish()

Note: If we are not passing any name then only default value will be considered.

After default arguments we should not take non default arguments


def wish(name="Guest",msg="Good Morning"): ===>Valid
def wish(name,msg="Good Morning"): ===>Valid
def wish(name="Guest",msg): ==>Invalid
SyntaxError: non-default argument follows default argument

Case Study:
def f(arg1,arg2,arg3=4,arg4=8):
print(arg1,arg2,arg3,arg4)

f(3,2)
f(10,20,30,40)
f(25,50,arg4=100)
f(arg4=2,arg1=3,arg2=4)
f() #Invalid TypeError: f() missing 2 required positional arguments: 'arg1' and
'arg2'
f(arg3=10, arg4=20, 30, 40) #Invalid SyntaxError: positional argument follows
keyword argument [After keyword arguments we should not take positional arguments]
f(4, 5, arg2 = 6) #Invalid TypeError: f() got multiple values for argument 'arg2'
f(4, 5, arg3 = 5, arg5 = 6) #Invalid TypeError: f() got an unexpected keyword
argument 'arg5'

You might also like