Functions in Python :
---------------------
Function: Function is a statement (or) group of related statements
which is used to perform specific task.
--> If it is required to write repeated no. of statements, we can keep
that repeated statements under one small unit, that unit we can call any
number of times when it is required.
--> That unit is nothing but function.
--> The main advantage of function is reusuability and avoiding repeatition
of the code.
--> Generally 2 types of functions are there in Python.
1. Built-in function
2. User defined function
1. Built-in function:
---------------------
It is by default defined by Python software foundation.
--> These functions are available by default when we install python software.
--> Built-in functions are case sensitive.
Ex-: print()
type()
id()
len()
eval()
2. User defined function:
-------------------------
--> These functions can be created by programmers according to
their requirements.
--> In Python to create a function, we use a keyword i.e def.
Syntax: def function_name(parameters):
--> def indicates the starting of the function header./ Initialise
--> Function name should be uniquely identify.
--> While giving function name we should follow some rules
of identifiers.
--> Parameters are the input values of function.
--> Docstring is description about function.
--> Any valid statements will make the function body.
--> Return statements is used to return the result to the calling place.
--> Parameters, docstring, return statements are optional.
#Creating a Function
def f1():
print("Welcome to my programming world")
#Calling the declared function
f1()
Function with one parameter-:
-----------------------------
def f1(name):
print("Hello", name)
f1("Sai")
f1("Bhabani")
** When function is having parameter while calling the
function, we should pass parameter value otherwise it will
give error.
Function with two parameter-:
-----------------------------
def add(a,b):
print("Sum is:", a+b)
add(20, 10)
add(40, 30)
Return Statements-:
-------------------
--> When we call the function, function will perform some task and
we call the function and return the result value to the calling
place using return statement.
def add(a,b):
return a+b
print("Sum is:", add(20, 10))
print("Sum is:", add(40, 30))
Question 1 -: Write a Python program using function to perform square
of a given number?
Ans 1:
------
def sqr(a):
sqr = a*a
return sqr
x = sqr(10)
print("Square is:", x)
(Otherway)
def square(n):
x = n*n
return x
print("Square is:", square(10))
Question 2 -:
-------------
Writa a Python program using function to determine whether
a number is even (or) odd?
def even_odd(n):
if n % 2 == 0:
print(n, "is even number.")
else:
print(n, "is odd number.")
even_odd(8)
Question 3 -:
-------------
Write a Python program to find biggest of two numbers
using functions.
def big(a, b):
if a > b:
print(a, "is big")
else:
print(b, "is big")
big(30, 20)
====================================================End of Python
Functions=======================================================
Created by: Prasannajit and Amit
1. What are functions in Python?
2. Please explain types of Function in Python?
3. What is a user defined function in Python?
4. Please explain the built-in function in Python?
5. How to create a function?
6. Why return statements used in Python?