0% found this document useful (0 votes)
4 views1 page

4.function in Python

The document explains the concept of functions in Python, highlighting the difference between built-in functions and user-defined functions. It covers how to create functions with and without parameters, demonstrating their reusability and the ability to perform specific tasks. Additionally, it discusses the return values of functions and provides examples of various function implementations.

Uploaded by

Aditya Tiwari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

4.function in Python

The document explains the concept of functions in Python, highlighting the difference between built-in functions and user-defined functions. It covers how to create functions with and without parameters, demonstrating their reusability and the ability to perform specific tasks. Additionally, it discusses the return values of functions and provides examples of various function implementations.

Uploaded by

Aditya Tiwari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

A function in Python is a block of code designed to do one specific task.

It helps make
your code cleaner and easier to manage. You can use a function multiple times without
rewriting the same code.
In [ ]:

Built-in Functions:
These are functions that are pre-defined in Python and are always available for use. Examples include print(), len(), sum(), id()
and type().
In [1]: print("hello world")

hello world

In [2]: len([3,4,5,6,6,8])

6
Out[2]:

In [3]: sum([3,4,5])

12
Out[3]:

In [4]: type(8)

int
Out[4]:

In [5]: min(4,5,6)

4
Out[5]:

In [6]: max(4,5,6)

6
Out[6]:

User-Defined Functions::
These are functions that users create to perform specific tasks. Functions which are created by programmer explicitly
according to the requirement are called a user-defined function. They are defined using the def keyword.

Once we create a function, we can call it anywhere and anytime. The benefit of using a function is reusability.
In [ ]:

Function Without Parameters:


In [ ]:

In [7]: def greet():


print("Hello, world!")
greet() # Calling the function without parameters

Hello, world!

In [8]: greet()

Hello, world!

In [9]: greet()

Hello, world!

In [ ]:

In [ ]:

In [10]: # def keyword is used to create or define a function.


def abc(): # abc is function name
print("this is function body")
print("This block of code is nothing but the action you wanted to perform.")

In [11]: abc() #function will execute when we call with name()

this is function body


This block of code is nothing but the action you wanted to perform.

In [ ]:

In [ ]:

In [12]: def add():


""" ADD of two number """
a = int(input("enter the first number = "))
b = int(input("enter the second number = "))
print(a+b) # this all is you created function with name add

In [13]: add()

enter the first number = 8


enter the second number = 2
10

In [14]: add() # u can use this function multiple time without rewriting the code

enter the first number = 6


enter the second number = 2
8

In [ ]:

Function with parameters


A function with parameters is a function that accepts one or more inputs (arguments) when it's called.

These inputs are passed to the function and can be used within the function's body to customize its behavior or perform operations on the provided data.

In [15]: def ad(a,b): # a,b is parameters Or you can say, parameters are variables in a function
print(a+b)
ad(3,8) # here 3,8 is arguments(values) ...which you pass into the function parameters

11

In [16]: ad(5,6) # here 5,6 is arguments(values) ...which you pass into the function parameters

11

In [17]: ad(58,69) # here 58,69 is arguments(values) ...which you pass into the function parameters

127

In [ ]:

In [ ]:

In [18]: def ar(l,b):


"""
Area of rectangle
"""
area=l*b
print("area of the rect is:-",area)

In [19]: l = int(input("enter the length = "))


b = int(input("enter the breadth = "))
ar(l,b)

enter the length = 6


enter the breadth = 1
area of the rect is:- 6

In [20]: ar(5,6)

area of the rect is:- 30

In [21]: ar(2,5)

area of the rect is:- 10

In [ ]:

In [ ]:

In [22]: def cov(kg):


pd=2.20462*kg
print("weight in pound", pd)

In [23]: a=int(input("Enter weight in kilograms: "))


cov(a)

Enter weight in kilograms: 6


weight in pound 13.227719999999998

In [24]: cov(7)

weight in pound 15.432339999999998

In [ ]:

In [ ]:

In [25]: def area(r):


a=3.14*r*r
print(a)
area(5)

78.5

In [26]: area(8)

200.96

In [27]: area(4)

50.24

In [ ]:

The value that a function returns to the caller is generally known as the function's return
value or ends the function call and returns the result to the caller.
In [28]: L = [3,4,5,6]
print([Link](7)) # The append() method doesn't return a value ...so that output is None

None

In [ ]:

In [29]: L = [3,4,5,6]
[Link](7)
print(L)

[3, 4, 5, 6, 7]

In [ ]:

In [30]: def add(y):


c = y
return c
print(add(400))

400

In [ ]:

In [31]: def add(y):


c = y
return c
print(add(400)+100)

500

In [ ]:

In [32]: def value(b):


a = b*4
return a

first_num = 4
print(first_num+value(4))

20

In [ ]:

In [33]: def add(y):


x = 100
c = x+y
return c
a = add(500)
print(a+200)

800

In [ ]:

You might also like