0% found this document useful (0 votes)
4 views4 pages

Functions in Python

A Python function is a reusable code block that performs a specific action, enhancing modularity and code reuse. Functions can be defined using the 'def' keyword, and they can accept parameters or arguments to process data. The document also explains how to create and call functions, as well as the distinction between parameters and arguments.

Uploaded by

MyDream 4U
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)
4 views4 pages

Functions in Python

A Python function is a reusable code block that performs a specific action, enhancing modularity and code reuse. Functions can be defined using the 'def' keyword, and they can accept parameters or arguments to process data. The document also explains how to create and call functions, as well as the distinction between parameters and arguments.

Uploaded by

MyDream 4U
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
You are on page 1/ 4

PYTHON - FUNCTIONS

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


perform a single, related action. Functions provide better modularity for
your application and a high degree of code reusing.
A top-to-down approach towards building the processing logic involves
defining blocks of independent reusable functions. A Python function may
be invoked from any other function by passing required data (called
parameters or arguments). The called function returns its result back to
the calling environment.

Types of Python Functions


Python provides the following types of functions −

Python Built in Functions


USER DEFINED FUNCTIONS
Defining a Python Function/Creating a Function
Syntax to Define a Python Function

def function_name( parameters ):


function body / Function statements
return [expression]

Example:
def greet():
print('Hello World!')
Creating a Function
In Python a function is defined using the def keyword:
def my_function():
print("Hello from a function")

Calling a Function
To call a function, use the function name followed by parenthesis:
def my_function():
print("Hello from a function")
my_function()

Arguments
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses.
You can add as many arguments as you want, just separate them with a
comma.
The following example has a function with one argument (fname). When
the function is called, we pass along a first name, which is used inside the
function to print the full name:
def my_function(fname):
print(fname + " Refsnes")

my_function("Emil")
my_function("Tobias")
my_function("Linus")

Parameters or Arguments?
The terms parameter and argument can be used for the same thing:
information that are passed into a function.

You might also like