In this video, I have explained regarding functions and user- defined in Python and started with the syntax and detailed explanation of it. Function is a piece of code which can be used in a code multiples times to perform a specific task. I have explained this part using a practical example where functions can be used. Loops are used to execute a piece of code multiple times so as to perform a particular task. The detailed explanation with an example is covered in this video.

FUNCTIONS IN PYTHON

A function is a group of statements defined to perform a specific task. It is a block of code which can be used multiple times to perform a related actions. It breaks your code and makes it more convenient, readable, manageable and organized.

As discussed in earlier videos, Python have many predefined or built in functions like print() , help() ,etc. This video and blog will explain you how you can create user defined functions in python.

How to define a function?

Syntax:

def functionname (arg1, arg2,…):

            Set of statements(s)

  • Every function block begins with def keyword which is followed by functionname and arguments which come under () round parenthesis.
  • Function can have as many arguments as user define and zero argument as well.
  • Every arguments are separated by a comma and after function is defined it is ended with colon.
  • Indentation is important for every function.
  • A function have return expression at last so to come out of the function block and come back to the point where the function was called and execute the remaining lines of code.

How to call a function?

def Welcome(name):

            print(“Welcome, ” + name + “!”)

            print(“Welcome to Electrofun”)

Once you are done defining the function, you can call it in program or python prompt. To call a function, type the function name with required parameters into it.

When you call Welcome(“NICK”), the above function will be called and you will get this output as follows:

Welcome NICK

Welcome to Electrofun

Flowchart:

There is a concept of local and global scope which is very important part when you work with functions. The concept of local and global variable will be explained in other blog and video respectively.