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 Syntax
Function blocks begin with the keyword
def followed by the function name and parentheses ( optional *)
def functionname(optional parameters ): "Body" 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") my_function()
Output: Hello I am a function
Parameters (Arguments) function
This example function has parameters. Passing a String value, which is used in a print().
def my_function(name): print(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
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))
Do Comment if you have any doubts and suggestions.
Note : This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All Tutorial on Python function and example are in Python 3, so it may change its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.