Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Dr R.

Madana Mohana

FUNCTIONS-1:
Introduction
Function Definition
Function Call
https://www.youtube.com/c/RASINENIMADANAMOHANA
Introduction

Function Definition

OUTLINE Function Call

Example Programs
Functions: Introduction
A collection of statements together into a single
unit. This unit is called a function.
In Python, function is a group of related
statements that perform a specific task.
If a group of statements is repeatedly required
then it is not recommended to write these
statements every time separately.
Functions: Introduction
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 nothing but function.
A function is a block of code which only runs when
it is called.
You can pass data, known as parameters, into a
function.
A function can return data as a result.
Advantages/Need of Python Functions

Code reusability because we can call the same


function multiple times.
Modular code since we can define different
functions for different tasks.
Improves maintainability of the code.
Abstraction as the caller doesn’t need to know
the function implementation.
To avoid repetition.
Need for Functions
Code reusability is one of the most prominent
reason to use functions.
Dividing the program into separate well defined
functions facilitates each function to be written
and tested separately.
Understanding, coding and testing multiple
separate functions are far easier than doing the
same for one large function.
Types of Functions
There are three types of functions in Python:
1. Built-in functions
2. User-defined functions
Built-in functions
Built-in functions: These functions provided by the
Python language. These function are coming from
along with python software internally .
Examples:
print(),len(),str(),list(),input(),type()
id(),max(),min(),int(),float(),bool(),
complex(),append(),remove(),get(),clear()
copy(),…..etc.
User-defined functions
User-defined functions:
The functions defined by us in a Python program.
These functions are defined by programmer as
per the requirements, are called user defined
functions.
FUNCTION DEFINITION
User-defined functions: Syntax
def function_name(argument-1,argument-2,..):
'''docstring'''
statement-1
statement-2
---
statement-n
return expression
FUNCTION DEFINITION
User-defined functions: Syntax
here,
def means function definition or function
declaration.
function_name is a name of the function.
argument-1, argument-2,..are list of formal
arguments/formal parameters.
':' indicates the beginning of the function.
FUNCTION DEFINITION
User-defined functions: Syntax
statement-1, statement-2,..are called the
body of the function or statement block.
return statement is used to return values
from functions to main program.
expression is value.
FUNCTION DEFINITION
docstring:
A docstring is a special comment at the
beginning of a user-defined function that
documents the function’s behavior.
docstrings are typically specified as triple-quotes
string comments.
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.
The syntax of calling function a function that does not
accept parameters, is simply the names of the function
followed by parameters i.e.
function_name()
function_name(argument1,argument-2,..)
FUNCTION PARAMETERS
A function can take parameters which are nothing but
some values that are passed to it so that the function can
manipulate them to produce the desired result.
These parameters are normal variables with a small
difference that the values of these variables are defined
when we call the function and are then passed to the
function.
Parameters are specified within the pair of parentheses
in the function definition and are separated by
commas(,).
FUNCTION PARAMETERS
Key points to remember while calling the function:
The function name and the number of arguments
in the function call must be same as that given in
the function definition.
If by mistake the parameters passed to a function
are more than that is specified to accept, then an
error willl be returned.
FUNCTIONS - Example Programs
Pgm-1: Write a function to find the addition of two numbers .
def addition(x,y):
z=x+y
return z
print(addition(10,20))
print(addition(6,9))
print(addition(4,36))

OUTPUT:
30
15
40
FUNCTIONS - Example Programs

Pgm-2: write a function to display a message


def hello():
print('welcome to Python Course')
hello()

OUTPUT:
welcome to Python Course
FUNCTIONS - Example Programs
Pgm-3: write multiple functions in a single program
def morning():
print('Good Morning')
def afternoon():
print('Good Afternoon')
def evening():
print('Good Evening')
def night():
print('Good Night')
morning()
afternoon()
evening()
night()
FUNCTIONS - Example Programs
Pgm-3: write multiple functions in a single program

OUTPUT:
Good Morning
Good Afternoon
Good Evening
Good Night
FUNCTIONS - Example Programs
Pgm-4: write a function to find the biggest of two
numbers
def biggest(x,y):
if x>y:
return x
else:
return y
a = int(input("Enter first number:"))
b = int(input("Enter second number:"))
big = biggest(a,b)
print('Biggest number =',big)
FUNCTIONS - Example Programs
Pgm-4: write a function to find the biggest of two
numbers
OUTPUT:
Enter first number20
Enter second number10
Biggest number = 20
Enter first number:20
Enter second number:30
Biggest number = 30

You might also like