0% found this document useful (0 votes)
65 views

Functions in Python

A function in Python is a block of code that performs a specific task. It takes in parameters and returns a value. Functions make code more organized and reusable. To define a function, we use the def keyword followed by the function name and parameters. The body of the function contains statements indented under the header. Functions are called by specifying the function name and arguments.

Uploaded by

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

Functions in Python

A function in Python is a block of code that performs a specific task. It takes in parameters and returns a value. Functions make code more organized and reusable. To define a function, we use the def keyword followed by the function name and parameters. The body of the function contains statements indented under the header. Functions are called by specifying the function name and arguments.

Uploaded by

ISR Educations
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

What is a function in Python?

In Python, a function is a group of related statements that performs a specific task. Functions
help break our program into smaller and modular chunks. As our program grows larger and
larger, functions make it more organized and manageable.
Furthermore, it avoids repetition and makes the code reusable.
Syntax of Function
def function_name(parameters):
"""docstring"""
statement(s)
Above shown is a function definition that consists of the following components.
1. Keyword def that marks the start of the function header.
2. A function name to uniquely identify the function. 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 the 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
the same indentation level (usually 4 spaces).
7. An optional return statement to return a value from the function.
Example of a function
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
How to call a function in python?
To call a function simply type the function name with appropriate parameters.
>>> greet('Paul')
Hello, Paul. Good morning!
Note: Try running the above code in the Python program with the function definition to see
the output.
def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")

greet('Paul')
The return statement
The return statement is used to exit a function and go back to the place from where it was
called.
Syntax of return
return [expression_list]
This statement can contain an expression that gets evaluated and the value is returned. If
there is no expression in the statement or the return statement itself is not present inside a
function, then the function will return the None object.
For example:
>>> print(greet("May"))
Hello, May. Good morning!
None
Here, None is the returned value since greet() directly prints the name and
no return statement is used.
Example of return
def absolute_value(num):
"""This function returns the absolute
value of the entered number"""
if num >= 0:
return num
else:
return -num
print(absolute_value(2))
print(absolute_value(-4))
Output
2
4
How Function works in Python?

Arguments
def greet(name, msg):
"""This function greets to
the person with the provided message"""
print("Hello", name + ', ' + msg)

greet("Monica", "Good morning!")


Output
Hello Monica, Good morning!
Here, the function greet() has two parameters.
Since the function is called with two arguments, it runs smoothly and no error will occur.
If we call it with a different number of arguments, the interpreter will show an error message.
Below is a call to this function with one and no arguments along with their respective error
messages.
>>> greet("Monica") # only one argument
TypeError: greet() missing 1 required positional argument: 'msg'
>>> greet() # no arguments
TypeError: greet() missing 2 required positional arguments: 'name' and 'msg'

You might also like