Functions that are easily part of Python are called inline functions. Python provides built-in functions like print ()
and so on, but we can also create your own functions. These functions are known as custom functions .
Table of Content
Custom Functions
All functions written by us fall under the Custom Functions category. Following are the steps for writing UDFs in Python.
- In Python, the
def
keyword is used to declare UDFs. - Indented statement blocks are followed by the function name and arguments that contain the function body.
Syntax :
def function_name (): statements. ...
Example :
|
Exit:
Inside function
Pair metric function
A function can take arguments (arguments), also called parameters, as input in opening and closing parentheses, immediately after the function name, followed by a colon.
Syntax:
def function_name (argument1, argument2, ...): statements. ...
Example :
|
Exit :
even odd
Default Arguments
Default Argument — it is a parameter that takes on a default value if no value is specified in the function call for that argument. The following example shows the default arguments.
Example :
|
Output:
x: 10 y: 50
Note. To learn more about default arguments, click here .
Key arguments
The idea is to let the caller side to specify the name of the argument with values so that the caller does not need to remember the order of the parameters.
Example :
|
Exit:
Geeks Practice Geeks Practice
Variable length arguments
We can have both a regular and a keyword with a variable number of args parameters.
- The special syntax
* args
in function definitions in Python is used to pass a variable the number of arguments to a function. It is used to pass a variable length argument list without a key. - The special syntax
** kwargs
in function definitions in python is used to pass a keyed variable length argument list. We use the double star name kwargs. The reason is that the double star allows us to pass through keyword arguments (and any number of them).
Example :
|
Exit:
Result of * args: Hello Welcome to Python.Engineering Result of ** kwargs mid == for first == Geeks last == Geeks
|
Exit :
Before calling function Value passed 10 Id 10853920 Array passed [1, 2, 3] Id 139773681420488 Inside function Value received 20 Id 10854240 List received [0, 2, 3] Id 139773681420488 After calling function Value passed 10 Id 10853920 Array passed [0, 2, 3] Id 139773681420488
Sometimes we may need the result of using a function in a later process. Therefore, the function must also return a value when it finishes executing. This can be achieved by return
.
The return statement is used to complete a function call and "return" the result (the value of the expression after the return keyword) to the caller. Statements after return statements are not executed. If the return statement has no expression, the special value None is returned.
Syntax :
def fun (): statements. ... return [expression]
Example :
|
Exit:
Result of add function is 5 Result of is_true function is True