Yet till now we have heard a lot about functions but still when the word “function” comes in our mind, it comes with lots of questions like
What is Function?
Why we use it?
How we can implement it….etc.
So, before understanding the functions, let’s revise what is a program? A Program is a set of instructions which perform a particular task. Now, sometimes we write a lot of statements in a single program which is very difficult to understand for programmer as they are complex. So, all the programming languages give us this functionality to break the programs into relevant blocks and name these blocks. Each of such block is called as a Function. We can divide a program into one or more functions, like :
- A function is basically a block of code that will execute only when it is called.
- A function can call another functions.
- We can call a function as many times as we want.
- Functions should always return a value either its user specific or NONE.
Benefits :
User Perspective:
- They help us divide our program into multiple tasks. For each task we can define a function. This makes the code modular.
- Functions provide a reuse mechanism. The same function can be called any number of times.
- It reduce program size.
- Functions make a program more readable and understandable to a programmer.
Computer’s Perspective:
- The Memory of the computer is less occupied and thus work more efficiently and smoothly while executing the program as now the program is being executed in form of small blocks (functions) rather than full code.
Types of Function
Built-in Functions :
These are predefined functions that are always available for use. For using them we don’t need to import any module. We can’t change their functionality as well as the their name. For Example : len(), type(), int(), input().
Function defined in Modules :
These functions are pre-defined in particular module and can only be used when corresponding module is imported. It is also similar to built in functions but for using them we need to import module. For example random(), sin(), sqrt()
User defined functions :
These are defined by the programmer. In this type of function user can give any name to the function as well as define or perform any task by this function.
Syntax of User Define Function :
Function Header :
The first line of function definition is called as the function header which contains :
- Keyword def that marks the start of the function header.
- A function name to uniquely identify the function. Function naming follows the same rules of writing identifiers in Python.
- Parameters : Variables that are listed within the parentheses of a function header, it is also known as Arguments. It is used to pass values to a function. They are optional.
- A colon (:) to mark the end of the function header.
Function Body :
The block of statements/ indented statements beneath function header that define the action performed by the function. Function body may or may not return any value. A function return a value through a return statement.
Indentation :
The blank space in the beginning of a statement within a block. All statements within same block have same indentation.
Functions without Parameters :
Now from the above topic, we get to know what is function, let’s create some functions but without any parameter.
Syntax of function without using Parameters :
Examples:
The above examples are of functions without parameters, but what we can see here is that there is no output.
Why So ? Logically, as per the syntax, we wrote the right code, so, what’s the problem, why it does not show any output?
Answer of the above problem is that FUNCTION cannot be EXECUTED until they are called. So, next question is, how can we call a function?
We can simply call a function by writing name of a function without using def keyword. A function can be called at any number of time like in above example we can call function 2 times in line no 6 as well as in line no 7.
When a function is called (i.e. when line no 6 will be executed) then control goes to the function (line no 1), executed body of function (i.e. line no 2 to 4) and control is returned back to the calling statement (line no 6), where the call originated. Same procedure will followed when we call this function again i.e. with line no 7.
So, in above example, if we talk about the order of the executions of the lines, then order is :
1 -> 6 -> 1->2->3->4->6->7->1->2->3->4->7
NOTE : Without calling a function, function is a waste of code in a program.
In conclusion, functions are an essential feature of Python programming. They allow you to organize your code into reusable blocks, making your programs more modular, efficient, and easier to understand. By breaking down complex tasks into smaller, manageable functions, you can write code that is more maintainable and easier to debug.
Let’s understand about different types of argument use by Python function in our next article “Types of Argument in Python Function”.