Skip to main content

Python Functions

Python functions are block of codes, Which they are executed on calling of function. In a function, we write a code for perform a task. A function  can also return the result as value.

The Python tutorial on Techtuts provides an information about Python and Python 3 from the basis to high level. This tutorial provides a good understanding about the Python and Python for Technical, Non-Technical People, College Students and Working Professionals. Given examples are an easy to write and simple to practice by yourself.

Simple Function

We can use the functions with or without passing arguments.  Simple function has a body of function only. For displaying simple message, we can use a simple functions without params or arguments.

Syntax for Simple python Function


#function definition
 

def function_name():

    #statements


#function call

function_name()

 

A simple function to display the welcome Message.

>>> def welcome_msg():
...     print('Welcome to Techtuts')
... 
>>> welcome_msg()
Welcome to Techtuts
>>> 

 

Syntax for Python function with arguments


#function definition
 

def function_name(arg1, arg2, ..., argn):

    #statements


#function call

function_name(arg1, arg2, ..., argn)

 

We need to use def keyword to create a function in python.

Functions with passing params / arguments

Params/ Arguments  are input for the function. For example if you want pass the name to the welcome message you can pass the name as function's param.

>>> def welcome_msg(name):
...     print(f"Welcome {name.title()} to tech tuts")
... 
>>> welcome_msg('jhon dave')
Welcome Jhon Dave to tech tuts
>>>