What is a function?
Function is a piece of code or small sub program that generally defined separately to perform a specific task. The very first use of defining a function is reusability. Once a function is defined, it can be invoked or called where and when necessary. Functions also divide the program into small chunks or modules and improves readability.
Two forms of any function
Definition
In general, a function can be seen in two forms in the program. One is the function definition and the other is the function invocation. The definition of a function is the specification of what is to be performed when the function has been invoked.
Invocation or Calling
The invocation of function is asking the function to perform what is specified in its definition. A well and prior defined function can only be called or invoked. The following are the examples of function definition and invocation.
#Example of function invocation
funcName()
funcName(x,y)
#Example of function definition
def funcName(Self):
executable code
executable code
Two important points related to functions
1. A function must have been defined before its calling
2. A defined function cannot start execution unless it is invoked or called.
Built-in vs User-defined Functions
Python language is providing many built-in or library functions like print(), input() which we already using in our previous programs. Remember the definitions of those functions are available with Python language libraries. Python is also allowing us to define our own functions which are called as user-defined functions.
Function Calling Mechanism
The function which is calling another function is known as caller function and the function which is being called is known as called function. When a function is called, the control transfers to the called function, which will be executed, and then transfers the control back to the caller function (to the statement following the function call).