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 , Ru...

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:        ...

Basics of Python Language

What’s python? Python is an interpreted Composite programming language.  Python is an object-oriented, high-level programming language with dynamic semantics.  Which holds high-level built-in data structures, it is a combination of dynamic typing and dynamic binding, make it extremely attractive for Rapid Development of Application,  It is used for the scripting language to connect existing components together.  It is simple and easy to learn syntax emphasizes readability and for that reason reduces the cost of program maintenance. Check here the Latest Updated Python Course Content  Why do people use python? Python is a Simple and easy language for beginners because python can have a wonderful community to help all the needs. The language is also rapidly growing domain and broadly taught in universities and used for functioning with learner-friendly devices such as the Raspberry Pi Some quotable quotes “You really enjoy coding with Pytho...