Functions in Python
Functions in Python
str=”John”
print(greet(str)) #Function Call
Output
Hello, John. Good morning!
Types of Functions
Functions are classified into the following two types:
1. Built-in functions/ Library functions
2. User-defined functions
Built-in Functions
Built-in functions are those that are pre-written in Python.
They are meant for use in a python code.
They cannot be modified or deleted.
There are 68 built-in functions in Python 3.6
Example: print( ), input( ), float( ), int( ), sqrt( ), hex( ), etc.
User-Defined Functions
User-defined functions are those that are written by users.
They can be modified or deleted.
Example: add( ), mul( ), fact( ), marks_display( ), etc.
Example Program
def cal_area(radius): #Function Definition
return 3.14 * radius * radius
print(cal_area(1.5)) #Function Call
Output
7.0649999999999995
Function Arguments
A function can be called by using the following types of formal arguments:
Required arguments
Keyword arguments
Default arguments
Variable-length arguments
Required arguments
These are the arguments passed to a function in correct positional order.
The number of arguments in the function call should match exactly with the
function definition.
To call the function printme(), you definitely need to pass one argument, otherwise
it gives a syntax error as follows.
Program:
def printinfo(name, age): #Function Definition
print("Name: ", name)
print("Age: ", age)
return;
printinfo(name="John", age=20) #Function Call
Output
Name: John
Age: 20
Keyword arguments
Keyword arguments are used in a function call, the caller identifies the
arguments by the parameter name.
These arguments need not be passed to a function in correct positional order.
Program:
def printinfo(name, age): #Function Definition
print("Name: ", name)
print("Age: ", age)
return;
printinfo( age=20, name="John" ) #Function Call
Output
Name: John
Age: 20
Default arguments
A default argument is an argument that assumes a default value if a value is
not provided in the function call for that argument.
The following example prints default age if it is not passed.
Program:
def printinfo(name, age = 25): #Function Definition
print("Name: ", name)
print("Age: ", age)
return;
printinfo(age=20, name="John") #Function Call
printinfo(name="Rahul") #Function Call
Output:
Name: John
Age: 20
Name: Rahul
Age: 25
Variable-length arguments
A function with more arguments than the specified arguments in function
definition..
Syntax:
def function_name( formal_args, *variable ):
function_suite
return [expression]
An asterisk (*) is placed before the variable name that holds the values of all non-
keyword variable arguments.
Program:
def printinfo( arg1, *a ): #Function Definition
print("Output is: ")
print(arg1)
for var in a:
print(var)
return;
printinfo(10) #Function Call
printinfo(70, 60, 50) #Function Call
First Output:
10
Second Output is:
70
60
50
Output:
Sum 1 : 30
Sum 2 : 30
Scope of Variables
Scope of a variable depends on the location where the variable is declared.
The scope of a variable determines the portion of the program where the
variable can access a particular identifier.
There are two basic scopes of variables in Python −
Global variables
Local variables
Global vs. Local variables
Variables that are defined inside a function body have a local scope. It can be
accessed only inside the function in which they are declared
Variables that are defined outside a function have a global scope. It can be
accessed throughout the program body by all functions
Output:
total = 0; # total is a global variable.
def sum(a, b): # Function Definition
total = a + b # Here, total is a local variable.
print("Inside the function local total : ",total)
return total
sum(10, 20) # Function Call
print("Outside the function global total : ",total)
Output:
Inside the function local total : 30
Outside the function global total : 0