0% found this document useful (0 votes)
7 views9 pages

Python Functions and User Defined Functions

Computer functions
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)
7 views9 pages

Python Functions and User Defined Functions

Computer functions
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

Surana Ind.

Pu College,
16, South End Rd, Bengaluru -560004

Chapter 7 - Functions

Topics
7.1. Introduction
7.2 FUNCTIONS
7.2.1 The Advantages of Function
7.3 USER DEFINED FUNCTIONS
7.3.1 Creating User Defined Function
7.3.2 Arguments and Parameters
7.3.3 Functions Returning Value
7.3.4 Flow of Execution
7.4 SCOPE OF A VARIABLE
7.5 PYTHON STANDARD LIBRARY
7.5.1 Built-in functions
7.5.2 Module

7.1 INTRODUCTION:
Modular Programming :

The process of dividing a computer program into separate independent blocks


of code or separate sub-problems with different names and specific
functionalities is known as modular programming.
7.2 FUNCTIONS
• Function can be defined as a named group of instructions that performs a
specific task
• Function can be called repeatedly any number of times.

7.2.1 The Advantages of Function

• The program is better organised and easy to understand.


• Reduces code length as same code is not required to be written at multiple
places in a program.
• Debugging will be easier.
• Increases reusability, as function can be called from another function or
another program
• Work can be easily divided among team members and completed in
parallel.

Page 1 of 9
[Link] Christopher Lee
Dept. of Computer Science
Surana Ind. Pu College,
16, South End Rd, Bengaluru -560004

Types of Functions :
• Built in functions (Standard Library functions)
o Set of pre-defined functions that are readily available for use
o Ex: print(), input()
• User defined function
o We can define our own functions. Such functions are called user
defined functions.

7.3 USER DEFINED FUNCTIONS


7.3.1 Creating User Defined Function

Syntax:

• A function definition begins with def (short for define).


• The items enclosed in "[ ]" are called parameters and they are optional.
• Function header always ends with a colon (:)
• Function name should be unique. Rules for naming identifiers also applies for
function naming.
• The statements outside the function indentation are not considered as part of
the function.

Example :
#function definition
def addnum():
fnum = int(input("Enter first number: "))
snum = int(input("Enter second number: "))
sum = fnum + snum
print("The sum of ",fnum,"and ",snum,"is ",sum)
#function call
addnum()

Page 2 of 9
[Link] Christopher Lee
Dept. of Computer Science
Surana Ind. Pu College,
16, South End Rd, Bengaluru -560004

7.3.2 Arguments and Parameters


Parameters : Value or variable passed in function header
Arguments : Value or variable passed in function call.

Example 1: (parameter and argument are different)


#function header
def sumSquares(n): #n is the parameter
sum = 0
for i in range(1,n+1):
sum = sum + i
print("The sum of first",n,"natural numbers is: ",sum)
num = int(input("Enter the value for n: "))
#num is an argument referring to the value input by the user
sumSquares(num)
#function call

Example 2: (No parameter and argument are passed)

#function definition
def addnum():
fnum = int(input("Enter first number: "))
snum = int(input("Enter second number: "))
sum = fnum + snum
print("The sum of ",fnum,"and ",snum,"is ",sum)
#function call
addnum()

Example 3 : (parameter and argument are same)


#function definition
def addnum(a,b):
c=a+b
print(c)
a=int(input(“enter a:”))
b=int(input(“enter b:”))
#function call
addnum(a,b)

Page 3 of 9
[Link] Christopher Lee
Dept. of Computer Science
Surana Ind. Pu College,
16, South End Rd, Bengaluru -560004

String as Parameters :
Not only interger values even string can be passed as an argument to the function

Example :
def fullname(first,last):
fullname = first + " " + last
print("Hello",fullname)
first = input("Enter first name: ")
last = input("Enter last name: ")
#function call
fullname(first,last)

Default Parameter
Parameters with predefined values.

Example :
def greet(name="Guest"):
print("Hello,", name)
greet() # Uses default value
greet("Kate") # Uses provided value
Output :
Hello, Guest
Hello, Kate

7.3.3 Functions Returning Value


A function may or may not return a value when called. The return statement returns
the values from the function.
1. Function with no argument and no return value:
2. Function with no argument and with return value:
3. Function with argument and no return value.
4. Function with argument and return value:

7.3.4 Flow of Execution


• Flow of execution can be defined as the order in which the statements in a
program are executed.
• The Python interpreter starts executing the instructions in a program from the
first statement.
Page 4 of 9
[Link] Christopher Lee
Dept. of Computer Science
Surana Ind. Pu College,
16, South End Rd, Bengaluru -560004

• The statements are executed one by one, in the order of appearance from top to
bottom.
• When the interpreter encounters a function definition,the statements inside the
function are not executed until the function is called. Later, when the interpreter
encounters a function call, there is a little deviation in the flow of execution.

In that case, instead of going to the next statement, the control jumps to the called
function and executes the statement of that function. After that, the control comes
back the point of function call so that the remaining statements in the program can
be executed. Therefore, when we read a program, we should not simply read from
top to bottom. Instead, we should follow the flow of control or execution. It is also
important to note that a function must be defined before its call within a program.

7.4 SCOPE OF A VARIABLE


The part of the program where a variable is accessible is called scope of that
variable

A variable can have one of the following two scopes


a. Global Scope
b. Local Scope

a. Global Variable: Variable that can be access outside the program is


called as Global Variable.
Any change made to the global variable will impact all the functions in
the program where that variable can be accessed.
b. Local Variable : Variable that can be accessed only within the function is
called as local variable.

Example :

num = 5
def myFunc1( ):
y = num + 5
print("Accessing num -> (global) in myFunc1, value = ",num)
print("Accessing y-> (local variable of myFunc1) accessible, value=",y)
myFunc1()
print("Accessing num outside myFunc1 ",num)
print("Accessing y outside myFunc1 ",y)

Global Variable : num


Local Variable : y
Page 5 of 9
[Link] Christopher Lee
Dept. of Computer Science
Surana Ind. Pu College,
16, South End Rd, Bengaluru -560004

7.5 PYTHON STANDARD LIBRARY

7.5.1 Built-in functions

Built-in functions : ready-made functions in Python that are frequently used in


programs

Some of the frequently used built-in functions in Python

Page 6 of 9
[Link] Christopher Lee
Dept. of Computer Science
Surana Ind. Pu College,
16, South End Rd, Bengaluru -560004

Commonly used built-in functions

Page 7 of 9
[Link] Christopher Lee
Dept. of Computer Science
Surana Ind. Pu College,
16, South End Rd, Bengaluru -560004

7.5.2 Module
• While a function is a grouping of instructions, a module is a grouping of functions.
• To use module, we need to import the [Link],we can directly use all the
functions of that module.

Syntax : import modulename1 [,modulename2, …]

(A) Built-in Modules :


some commonly used modules and the frequently used functions that are
found in those modules:
• math • random • statistics

Page 8 of 9
[Link] Christopher Lee
Dept. of Computer Science
Surana Ind. Pu College,
16, South End Rd, Bengaluru -560004

*******************

Page 9 of 9
[Link] Christopher Lee
Dept. of Computer Science

You might also like