Skip to main content

Functions in Python



In this part of the tutorial, we are going to discuss about Function in python. Before going into it, Let’s have a quick recap to the Python Basics we had previously completed.

Learn Python from scratch by our Expert instructor Start Now!



When programming, you will encounter the calculation and logical operation you need to perform  repeatedly. One way to handle this is to write the same code over. A better solution is to write a function, function enable you to reuse the logic an infinite number of times without repeating yourself.

Time to begin,

Definition

A function is a block of code which runs in a certain oreder  is called it by name. It can be passed data to operate on  the parameters and can return value. All data that is passed to a function is explicitly passed.

Benefits of Python Function

  •          Reuse of code
  •          Reducing duplication of code
  •          Decomposing complex problems into simpler pieces
  •          Improving clarity of the code
  •          Information hiding

Basic Syntax of Function

def function_name(parameters):

                """docstring"""

                statement(s)

Let see,

How to create a function in python?

A function is defined by using def keyword

Here’s one

To define a function, you write def followed by the name of the function

def my_function(name):

print(“Hi ,”+name+ “ .  Happy Morning”)

How to call a function?

Once we have defined a function, we can call it from another function. To call a function  simply use  the function name with appropriate parameters.

>>> myfunction('Yuvi’)

Hello, Yuvi. Happy morning!

Docstring

The first string after the function header is called the docstring and is short for documentation string. It is used to explain in brief, what a function does.


The  Return Statement

The statement return execute a function and passing back to the  place where it was called

Syntax  : return [expression_list]

Example,

def sum( arg1, arg2 ):

 # Add both the parameters and return them."
   total = arg1 + arg2
   print "Inside the function : ", total
   return total;

# Now you can call sum function
total = sum( 10, 20 );
print "Outside the function : ", total

Output :

Outside the function :  30
Inside the function :  30

Check out :  Installation of Python


Scope of Variable

All variables in a program that cannot be accessible at all the location so it based on where you have declared a variable. There are two basic scopes of  variables in python ,
  •          Local variables
  •          Global variables

Local and Global Variable

Scope of a variable is the portion of a program where the parameters and variables defined inside a function is not visible from outside. Hence, they have a local varaibles.

On the otherhand, Global variables can be accessed through the entire program body by all functions.

def my_func()
x = 10
print("Value inside function:",x)
x = 20
my_func()
print("Value outside function:",x)

Output :

Value inside function: 10
Value outside function: 20






Comments

Post a Comment

Popular posts from this blog

Python Interpreter - Difference between Compiler & Interpreter - Python Tutorials

In our series of Python tutorials previously we discussed about the Basics of Python and Installation of Python in video tutorials as well. Here in this part lets discuss about Python Interpreters. Interpreter and Compiler:             As a software developer, we use different types of programming languages such as C, C++, Java, Python depends upon our requirement. We all know that the programming languages which a human can understand are called as high-level programming language. But: The computer understands only machine learning, Hence it is needed to convert or translate the code into the machine language. Interpreter and Compilers are used to convert the source code into the machine code. What is an Interpreter? The interpreter is nothing but a program which reads and executes the code. It includes different types of codes such as Source code, Compiler code . Most common interpreters available are Python , Ruby and PERL in which they execute their respective co

Python Basic Syntax

Introduction:  Python is most popular and high level programming language designed by Guido van Rossum in the year of 1991. You can use it to build web development, Game Development, Machine Learning Algorithm, etc and also You can even make robots using python.             Basic syntax of a python program is easy and simple than other programming languages.Python programs can be written using any text editor and should have the extension .py.             Before going further into Python syntax,I recommend you to read about the basics of Python . This Python Tutorial will explain about the basic syntax in Python with example programs. FREE PDF – Python course content. First Python Program:           Type the following content at the Python prompt and press the Enter:  print “Welcome”  The output will be  - Welcome Comments in Python:            Comments are denoted by # Python Comments starts with a # Character.Multiple line comments do not exist in Python. Example:

Understanding Python Interpreter

How Python Runs Programs? Normally, programmers write their own python scripts with the .py extension, that are independent in the python environment. the".py" extension, which tells the operating system that the file is Python program this need to be executed in python.  After that OS decides and the interpreter is invoked, it reads the file and interprets the file. The way in which Python scripts are run on Windows and other operating systems is very different. How You Run Programs?  If you are already known to run some other languages like C, C++, Java, It is simple to run the program using an interpreter, you can able to run either command prompt or using the IDLE. Before that check here to know How to Install Python? Following are the steps to run the python file on your own,  Step 1: Open the IDLE of python and then type your code just press enter the result will be printed on your screen. Step 2: Run Python Script open the new file in the fil