Skip to content

Python Functions Basics Tutorial with Example

  • by

Python functions are used to keep your code more organized and reusable. Functions as small chunks of code that together build a program. It provides better modularity for applications and more code reusability.

In Python Functions, you can pass data, known as parameters. A function can return data as a result or not, depends on requirements.

Python Functions Basics Tutorial with Example create

Python Functions Syntax 

Function blocks begin with the keyword def followed by the function name and parentheses ( optional *)

def functionname(optional parameters ):
   return [expression]

Python Functions Basic example 

This is a simple function example, for calling the function just write the name of the function.

def my_function():
    print("Hello i am function")


Output: Hello I am a function

Parameters (Arguments) function 

You can pass Strings, Numbers, etc objects in functions. You can add as many parameters you want and separate them with a comma (parm1 , parm2 , ....).

This example function has parameters. Passing a String value, which is used in a print().

def my_function(name):

my_function("Eyehunt Tutorial")

Output: Eyehunt Tutorial

You can do the same for numbers.

Return type function

For a function return a value, have to  use the return statement:

In this example, we are passing Number (Integer) data type and get the returned sum of those 2 numbers in function.

def my_function(no1, no2):
    sum_ = no1 + no2
    return sum_

print(my_function(3, 4))

Output: 7

Do Comment if you have any doubts and suggestions.

Note : This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6

Python 3.7

All Tutorial on Python function and example are in Python 3, so it may change its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading