Functions are a block of code that does a specific task. It saves the time of the developer as a full block of code can be executed by just calling the function. Functions can be broadly classified into two groups i.e. Built-in Functions, example print(), round(), etc. and User-defined Functions in Python which are defined to work as per user’s needs.
In Python, we can declare a user-defined function by the keyword def followed by the function name. The function may take arguments as input within the opening and closing parentheses, just after the function name followed by a colon. Once we have defined the function name and arguments, a block of program statement starts at the next line and these statements must be indented.
Call a Function
In Python, calling a function is pretty simple, using the function name, parenthesis (opening and closing) and parameter(s) if required. See the
How are you ?
In the above example,
defined a function Hello without parameters. This function is programmed to
“Hello World. How are you?” whenever called.
Advantages of user-defined functions
- User-defined functions help to decompose a large program into small segments which make the program easy to understand, maintain and debug.
- These functions are very useful, from writing common utilities to specific business logic. These functions can also be modified per requirement.
- If repeated code occurs in a program. A function can be used to include those codes and execute when needed by calling that function.
- Large project workload can be divided by making different functions.
Functions with arguments
In Python, user-defined functions can take four types of arguments. The argument types and their meanings, however, are predefined and can’t be changed. A developer has to follow these pre-defined rules to make their own custom functions. Below are the 4 types of arguments
- Default Arguments
- Required Arguments
- Keyword Arguments
- Variable length Arguments
In Python, the function arguments can have default values. If the Default Argument function is called without passing any argument, the argument takes its default value. The default value for the Function is assigned by assignment
In the above example, we have defined a function def_func with the default argument greeting as Hello. This function is programmed to print “Hello ”, whenever called and no parameter is passed into greetings. When we called the function the second time we passed
“Welcome” into argument greetings, this time greetings argument will take the passed parameter.
In Python, the required arguments are the mandatory arguments of a function. while calling the required argument function we have to pass the correct number of arguments along with their correct order. If we do not pass all the arguments or pass arguments in the wrong order, the function will fail.
My name is Harry
My age is 12
In Python, Keyword Argument Function in Python is the same as a required argument, the difference is we mention the keyword during the function call along with the corresponding values. We do not need to maintain the order of the arguments. Below we will consider the same example we used for required arguments.
My name is Harry
My age is 12
Variable length Arguments
In Python, Variable-length argument is used when we do not know the number of arguments to be passed. to create a variable length function we have to pass *args as an argument while creating the Function.
this is Var len Function
The Return Statement
The return statement is used to return the required variable to the code and exit a function and go back to the place from where it was called. In all the above example we have not used a return statement. Therefore we can conclude that the return statement is not mandatory. The values returned by the function can be used later in the code.
It is possible that a return statement contains an expression that gets evaluated and the value is returned. If there is no expression in the statement, then the function will return the None object.
product of x & y is 20
Please comment your question related to this topic.
View previous post IF, IF ELSE, ELIF Statements