In this tutorial, we are going to discuss python functions. We will learn how to write a function and how to use it?
Table of Content
- What are Functions in python
- Types of function
- The return type of function
- Scope of python
What are the functions in python?
A function is defined as a collection of code which is used to perform a specific task. Functions are used as re-using of one code again and again. Functions are also known as modules. With the help of function, the reusability of code become popular.
Every programming language has the concept of function and function works on a principle DRY (Don’t repeat yourself).
Types of Functions in python:
There are two types of functions in python
- Built-in functions
- User-defined function.
All those functions which are already defined in python are known as python inbuilt functions.
Some examples of inbuilt functions:
print() input() str()
All those functions which are created by the user to perform a specific task are known as user-defined functions. To write a function we use a keyword def.
Syntax to write a user-defined function:
def function_name(parameters): '''docstring''' function_body or statement
Some points related to function:
- to define a function, we use a keyword def before every function name.
- every function should have a function_name which end with the parentheses.
- Inside the parenthesis, we can pass the parameters which are optional.
- The docstring is a string inside the function between the triple single quotes which is used to give the information about the function.
- Inside the function, you can use the return statement which is optional.
- To call a function we write the function name with the parenthesis.
Let’s all the concept of Python function with an example.
User-defined function with no return type and no parameters which are optional
def helloworld(): '''this function use to print hello world''' #docstring of function print("Hello world") helloworld() #calling function helloworld
Behind the code:
In the above example
- We have used the def keyword before the function name
- The function name of the above example is
- To call the function we write the helloworld() outside the function.
- The many times you type the helloworld() outside the function that many times you get an output Hello world
Docstring in python:
In function, docstring is used to give brief information about what the function does. It is basically a string between triple quotes and written just after the header of function.
It likes a comment that does not execute but you can call it when you need information about the functions.
To call a docstring we use an attribute __doc__ along with the function name.
Syntax to print a docstring
let’s understand it with an example:
def helloworld(): '''this function use to print hello world''' #docstring of function print("Hello world") print(helloworld.__doc__) #printing the document string of the function helloworld #Output
this function use to print hello world
Return type function in python:
return keyword in function works like a break keyword in loops but the difference is the return keyword return a value which we can print using the print function.
why do we compare the return keyword with the break, because the moment in the function the interpreter finds the return keyword it does not interpret the further function statement which is written after the return keyword it throws us out of the function with the value which is written along with the return keyword.
Let’s understand it with an example
def sum_num(): #two parameters a and b ''' the function sum_num return the sum of two value''' a = 4 b = 5 sum = a + b return sum print(“hello world”) #this statement will not execute. print(sum_num())
In the above code, the function name is sum_num which is adding 2 numbers a and b and the name is given to their sum is sum. You can also see that this time we have used a return keyword and when we are calling our function instead of just calling it, we are printing it why?
This time the function sum_num is returning a value and the value is sum the addition of names a and b you can say that instead of printing the function sum_num we are printing the sum.
There is a print(“hello world”) statement after the return sum that did not execute because when the functions find the return statement they stop executing further.
Scope and lifetime of function variables
The scope refers to the area where the variable is visible and free to use. For example, the variable inside the function is limited to function itself you cannot use it outside the function.
The lifetime of a variable refers to the period of a variable how long it’s in the memory. For example, the lifetime of the function variable is as long as the function executes the moment the function return something and stop executing the lifetime of the function variables end.
Let’s understand it with an example:
def scop_lifetime(): variable = 20 return(variable) print(scop_lifetime()) variable = 40 print(variable)
- Keyword & Identifiers
- Variables, Constraints, & Literals
- Python Comments, Statements, Indentation & Docstring
- Python Type Conversion & Type Casting
- Python Input Output Import
- Python Name, Namespace, & Scope
- Python If, Elif
- Python For Loop
- Python While Loop
- Python Continue & Break Keywords
- Python Pass Keyword