Functions in Python Programming With Examples

Functions in Python Programming With Examples

In this article, you will learn about what is functions in Python programming, the syntax, function components and types of function with examples for your practice.

In this article, you will learn the following things.

What is a Function in Python Programming?
Syntax of Function
Example of a Function
How to call a Function in Python Programming?
Docstring in Python 
The return statement
Syntax of the return statement
Example of the return statement
Scope and Life of variables

What is a Function in Python Programming?

In Python programming, a section of related statements in a  program that performs some specific task is called a function.

A big advantage of using a function in Python programming is that the functions divide a program into smaller modules or chunks. We can assign a section of our program with a distinct function name so we can call or use that part of the program wherever we want. and makes the codes reusable easily.

Moreover, it is much convenient for the programmer to handle the program when it grows larger in size and it becomes more manageable.

Syntax of Function

There are following things need to be noted in the above function syntax.

  • The very first thing is the keyword “def” which means the starts of the function header.
  • The function name should be uniquely identifiable and should follow the same naming convention as for variables in Python programming.
  • Function arguments or parameters is an option that helps us in passing a value to a function. They can be added or not as per requirement.
  • A (:) colon sign is mandatory at the end of the function header to tell the Python programming that the function header ends here.
  • By docstring, it means the documentation string that is used to show what the function does.
  • The function body where all the needed lines of code, loops and all the required stuff comes.
  • In Python programming, you should be cautious about the use of the indentation level. For this, you can read about how to use indentation in identifiers.
  •  In the end, an optional return statement.
Recommended:  Variables,Data Types and Type Conversion In Python Programming Language

Example of a Function

Output:

Recommended:  How To Use Input Functions In Python Programming Language

How to call a Function in Python Programming?

A function can be called from another function, program or even Python shell. To call a function simply we have to type the name of the function with appropriate arguments or parameter names as we do in the above example.

Docstring in Python

Docstring in Python programming is short for document string is an optional documentation string. It can be used as a hint or as a reminder for the developer. Using docstring is a good programming practice.

Docstring comes first after the header in the function. Generally triple ( “””) quotes to use to extend the docstring to the multiple lines.

In the above example, we use docstring immediately after the function definition (def my_function(fname):).

The return statement

The purpose of using the return statement is to terminate or exit the function.

Syntax of the return statement

Example of the return statement

Output:

Scope and Life of variables

Scrope or working capacity of a variable is the limit in a block or program until when the variable is able to work or call. All the variables or identifiers who have local scope usually defined inside the function and cannot be accessed from the outside of a function. They are called local variables.

Recommended:  Break And Continue Statements In Python Programming Language

By the life of variables, we mean a time period through which the variable can be stored in the memory and this usually as long as the function in which it defined executes.

And once the function gets terminates the variables stored in the memory destroyed.

Other than local variables there is another type of variable scoping which is global variables. These types of the variable defined outside of function body and can be called from the outside of a function or inside of a function.

Below is the example for a better understanding of the scope of a variable.

Output:

 

Jack Sparrow

Hay I Am Admin Of This Website. I Am Very Passionate About Computers And Technology. This Website Is The Result Of My Passion. I Love To Eliminate The Barriers That Come In Seeking Education. This Website Is The Free Source Of Learning About New Technology And Up To Date Knowledge.

Leave a Reply

Close Menu