Functions in Python
What is Function?
In Python, a function is a set of related statements that perform a specific task. Functions help to divide our program into smaller and more modular parts. As our programs become larger and larger, these features make them more organized and clear. It also avoids duplication and allows code to be reused.
Syntax of Function
def function_name(parameters): """docstring""" statement(s)
Keyword def, used to mark the beginning of the function header.
Function name, used to uniquely identify the function. Function names follow the same rules for writing identifiers in Python.
We use it to pass values to the parameters (arguments) of the function.
The colon (:) is used to mark the end of the function header;
The optional return statement is used to return a value from a function.
Creating a function
def my_function(): print("Hi this is python function")
Calling a function
def my_function(): print("Hi This is function") my_function()
Hi This is function
Difference between Arguments and parameters
These two terms are very interchangeable, so it is not so important to know the difference. The terms they refer to are almost identical. However, in order to sound more professional, correct terminology is important. Variables that are in brackets when defining the function. When a method is called, the arguments are the data passed to the method’s parameters.
Function arguments : Arguments are used to pass information from the rest of the program to the function. This information return a result. There is no limit to the number of arguments that can be written. Depending on the type of function you’re performing, there might even be no argument.
Use commas to separate the arguments in a function. Take into account the number of arguments you put in a function call. The number of arguments must be exactly the same as the number of parameters.
In the example below, the variable name is the input parameter, where as the value, “Arnav”, passed in the function call is the argument.
def welcome(name): print("Hello! " + name + " Good Morning!!") welcome("Arnav")
Hello! Arnav Good Morning!!
Returning a Function
If you wish to return some values from the function to the rest of the program, you can use the return statement. As the name suggests, it returns a value without printing it. Executing this statement will cause the Python function to end immediately and store the value being returned into a variable.
How function works
Scope and Lifetime of variables
When dealing with Python functions, you should also consider the scope of the variables. The code in a function is in its own little world, separate from the rest of the program. Any variable declared in the function is ignored by the rest of the function. This means that two variables with the same name can exist, one inside the function and the other outside the function. However, this is not a good practice. All variable names must be unique regardless of their scope.
Local variable: A variable that is defined within a function and can only be used within that particular function. With respect to the local variable, the area in a function is called “local area”.
Global variable: A variable that is defined in the main part of the programming and, unlike local variables, can be accessed via local and global areas.
Types of Functions
Basically, we can divide functions into the following two types:
Built-in Functions – Functions that are Built-into Python.
User-Defined Functions – Functions that are defined by the user themselves.