![]() |
Python Functions - Table of Content
Become a Python Certified professional by learning this HKR Python Training!
A user has to follow various steps to run a user defined function in python.
1.Creating a function
Below is a basic syntax for creating a function in python:
def function():
print("Welcome to HKR Training")
2.Calling a function
The user can call the function by calling its name having parenthesis which contains the parameters of that function. This is done once the basic structure of a code is finalized.
Below is a python code to depict the calling of a function :
def function():
print("Welcome to HKR Training")
function()
Output of the python code:
Welcome to HKR Training
An important thing to note while calling a function is that there cannot be more than one value for a parameter. Also, the order of arguments is very important.
Arguments in python are the values that are passed inside the function parenthesis. There is no limit to how many arguments can be. A user can have as many arguments inside a function as he wants, separating it by a comma.
Below are 3 types of arguments that a python function can have:
Python default arguments: A python function has parameters which can further have default values. Default value will be taken up only if no argument value is passed during calling of the function
Python keyword arguments: Unlike default arguments, python also allows writing an argument in desired order if the user names the argument while calling the function. These arguments provide flexibility and good control over the values in a function
Python arbitrary arguments: These arguments come into place when the user is not sure of how many arguments are required by a function. This generally happens while calling a function as user gets unsure of how many arguments a function will accept. So, arbitrary arguments are passed by inserting an asterisk (*) before the function parameter to state that function can now take up arbitrary number of arguments.
Below is a python code for calling the function using keyword arguments:
def child(firstname, lastname ='Kaur', standard ='Third'):
print(firstname, lastname, 'studies in', standard, 'Standard')
# First keyword argument
child(firstname ='Aman')
# Second keyword argument
child(firstname ='Rama', standard ='forth')
# Third keyword argument
child(lastname ='Singh', firstname ='Babli')
Output of the python code:
Aman Kaur studies in Third Standard
Rama Kaur studies in forth Standard
Babli Singh studies in Third Standard
>
Top 30 frequently asked Python Interview Questions!
Here in the above code, we can analyse that only one keyword argument is required in the first call. Then there are two arguments in the second call where one is required call and second is optional. The value will get replaced to a new passing value from the default one. We can also figure out that sequence of keyword argument is not essential in the third call.
While working with python functions, a user might come across a lot of unwanted errors such as calling invalid functions calls. Here are some tips which can save you from the same. Let us start understanding this with a python code first:
def child(firstname, lastname ='Kaur', standard ='Third'):
print(firstname, lastname, 'studies in', standard, 'Standard')
# Missing required argument here
child()
# non keyword argument after a keyword argument
child(firstname ='Rama', 'forth')
# unknown keyword argument
child(subject ='English')
Output of the above python code:
File "", line 8
SyntaxError: positional argument follows keyword argument
>
This python code clearly throws up an error. Here are the reasons:
The correct python code is shown as code 2 where all the arguments and values are passed correctly.
The immediate next string after a function is called as document string or can also be called as Docstring. The main purpose of docstring us to state the functionality of the function. However, it is not mandatory to always use a docstring.
Below is a basic syntax for printing a docstring to a function:
print(function_name.__doc__)
Return statement is used to take an exit from a function and directly move back to the caller of the function. It returns the specified value to the caller. The return statement can only have a variable, a constant or an expression otherwise nothing is returned with the return statement. The syntax for the same is stated given below:
return [item_list]
Related Articles
As a senior Technical Content Writer for HKR Trainings, Gayathri has a good comprehension of the present technical innovations, which incorporates perspectives like Business Intelligence and Analytics. She conveys advanced technical ideas precisely and vividly, as conceivable to the target group, guaranteeing that the content is available to clients. She writes qualitative content in the field of Data Warehousing & ETL, Big Data Analytics, and ERP Tools. Connect me on LinkedIn.
Batch starts on 8th Dec 2023 |
|
||
Batch starts on 12th Dec 2023 |
|
||
Batch starts on 16th Dec 2023 |
|