In this tutorial, we will discuss what python functions are, how to write a function and how to use it? So let's discuss Python Function.
What are the functions in python?
With the help of function, the reusability of code become popular. A function is defined as a collection of code used to perform a specific task. Functions are used as re-using of one code again and again. Functions are also known as modules. 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.
Built-in functions: All those functions already defined in python are known as python inbuilt functions. Some examples of inbuilt functions:
print() input() str()
User-defined functions: All those functions that the user creates to perform a specific task are user-defined. 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 ends 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 concepts 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 the 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 return keyword returns a value that 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 adds 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)
- Getting Started
- Keywords & Identifiers
- Statements & Comments
- Python Variables
- Python Datatypes
- Python Type Conversion
- Python I/O and Import
- Python Operators
- Python Namespace
- Python if...else
- Python for Loop
- Python While Loop
- Python Break and continue
- Pass Statement
- Looping Technique
- Function Argument
- Python Recursion
- Anonymous Function
- Python Global, Local and Nonlocal
- Python Global Keyword
- Python Modules
- Python Package
- Python Numbers
- Python List
- Python Tuple
- Python String
- Python Set
- Python Dictionary
- Python Nested Dictionary
- Python Arrays
- Python Matrix
- List Comprehension