Skip to main content

FUNCTIONS IN PYTHON



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.

Comments

Popular posts from this blog

COMPARISON, LOGICAL, ASSIGNMENT AND BITWISE OPERATORS

COMPARISON, LOGICAL, ASSIGNMENT AND BITWISE OPERATORS In this blog we will see about Arithmetic operators and understand how to use it with Python IDLE to perform certain calculations. PYTHON COMPARISON OPERATORS OPERATOR DESCRIPTION EXAMPLE        == Checks whether LHS is equal to RHS, then the condition becomes true 10==10 is true        != Checks whether LHS is not equal to RHS, then the condition becomes true 2!=3 is true        > Checks whether LHS is greater than RHS, then the condition becomes true 6>3 is true        < Checks whether LHS is less than RHS, then the condition becomes true 6<10 is true        >= Checks whether LHS is gre...

BUILT INS FUNCTIONS IN PYTHON

BUILT INS FUNCTIONS IN PYTHON Every programming languages have certain pre-defined functions. Similarly, for certain tasks python has built in functions which you can access and understand as explained in the video.   dir(__builtins__): This module will return all the built in function and identifiers directly available to the programmer.   help(“function name”): This is the most important function in python. It gives the complete detail regarding the function which you mentioned inside help().

WHILE LOOP IN PYTHON

WHILE LOOP IN PYTHON Loops are used to execute a piece of code or set of statements multiple times in a program as long as the condition is true. While loop is the basic loop structure in Python. Syntax: While (expression):             Statement(s) Here, condition may be any logical expression and statement(s) can be single statement or multiple statements. The loop will iterate or execute until the condition is true. When the condition becomes false, program comes out of the loop and remaining statements are executed. In Python language, during any of the decision making, control statements and looping indentation is very important because, it specifies the set of statements to be a part of single code or block. Since no brackets are used to specify the complete code with in, indentation is used. Flowchart: In While loop, the main important thing is the loop may never be executed if the condition is...