Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Functions in Python

The document explains the concept of functions in Python, detailing their definition, syntax, types, and how to call them. It covers built-in and user-defined functions, various types of function arguments, the return statement, and the scope of variables. Additionally, it provides examples to illustrate these concepts.

Uploaded by

NivedhithaV
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Functions in Python

The document explains the concept of functions in Python, detailing their definition, syntax, types, and how to call them. It covers built-in and user-defined functions, various types of function arguments, the return statement, and the scope of variables. Additionally, it provides examples to illustrate these concepts.

Uploaded by

NivedhithaV
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Function

 A function is a block of organized, reusable code that is used to perform a


single, related action.
 Functions provide better modularity and a high degree of code reusing.
Defining a Function
Rules to define a function in Python are
1. Keyword def marks the start of function header.
2. A function name is to uniquely identify. Function naming follows the same
rules of writing identifiers in Python.
3. Parameters (arguments) through which we pass values to a function. They
are optional.
4. A colon (:) to mark the end of function header.
5. Optional documentation string (docstring) to describe what the function does.
6. One or more valid python statements that make up the function body.
Statements must have same indentation level (usually 4 spaces).
7. An optional return statement to return a value from the function.
Syntax
def functionname( parameters ):
"function_docstring"
function_suite
return (expression)
Parameters have a positional behavior and they are passed in the same order that
they were defined.
Calling a Function
 Function definition gives a function name and parameters.
 A function can be invoked by calling it from another function or directly from
the Python prompt.
def greet(name): #Function Definition
print("Hello, " + name + ". Good morning!")

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

The return Statement


 The statement return[expression] exits a function, optionally passing back an
expression to the caller.
 A return statement with no arguments is the same as return None.
Program:
def sum(a, b): #Function Definition
total = a + b
print("Sum 1: ", total)
return total;
total = sum(10, 20); #Function Call
print("Sum 2: ", total)

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

You might also like