User defined functions in Python
The function is a set of statement that takes the input and do specific task and produce the output. The functions are common programming language and can be defined as block of reusable code to perform specific task. So instead of writing same code again and again for different inputs we call the function. In python all functions are treated as objects so more flexible compared to other languages. Python include many build in functions. They are used to perform a predefined task and called as per requirement. Creating of own function in python is called user defined function. These functions can be modified as per requirement.
The codes are well organized, easy to maintain and developer friendly means can support modular design approach.
The codes are written independently and tasks of project can be written used for application development.
User defined functions have advantages as that when to use and how.
In this the codes are reusable codes they are written only once and used multiple times can be used in many other applications.
Importance of User defined functions in Python:-
You use functions in programming to bundle set of instructions and use it repeatedly.
Function is piece of code written to carry a specific task.
The developers can write user defined function or can be taken by other (third party) libraries.
This means that your own function can also be third party for other users.
- Reducing duplication of code
- Decomposing complex problems into simpler pieces
- Improving clarity of the code
- Reuse of code
- Information hiding
Defining of function:-
Defining function in python means first built in and then user defined.
- Build in (print ()).
- User defined.
Build in function are part of python package and libraries.
User defined functions are written by developers to meet certain requirements.
- Use def keyword to declare function name.
- Write the argument inside the opening and closing parenthesis of function.
and end your line with colon.
- Add statement that to be executed.
- End your function with return statement if function should output something.
- And without return statement your function will return an object none.
Remember that you can define one or more function parameters for user defined functions.
def function():
pass
def keyword is followed by function name with colon and round bracket.
The function is executed when needed called as “calling the function”.
They are also not executed until the function is called.
myfunc()-used to call function or specify the function name with round brackets.
e.g:-
def sayhello():
“””
This prints hello
“””
print(“hello”)
print(“python”)
Calling the function:-
Defining a function only giving the name, specify the parameters that are to be included in the function and structure block of code.
Once the basic structure of a function is finalized.
You can execute it by calling it from another function or directly from the Python prompt
After defining we call function from another function, program.
For calling we need only type of function name with parameters.
Calling mean you execute the function that you need to have defined directly from.
Python Function calling without argument and no return statements:-
Function with no argument is called as function that will not receive any data from calling function and function with no value means calling function does not receive any data from called function.
The arguments of a function are defined as def statement .In python there is no explicit type associated with function argument.
The function argument can be defined with default values in function.
Some programming language python does not pass reference to instance of class.
The function has no arguments it will not receive any data from the calling function. When it does not return a value the calling function does not receive any data from the called function.
Syntax:
Function declaration: void function ();
Function call: function ();
Function definition:
Void function ()
{
Statements;
}
e.g:-
sayhello()
O/p:-
Hello
Python
The above example will not contain any argument in it. The say hello is seceded with empty parenthesis.
There is absence of parameter here.
The body of function is written inside the definition of function.
Major and main advantage of function is that it can be created once and used /called many times.