Hey folks! We have brought you a new topic in which we will discuss about the Python functions, types of python functions and arguments of functions.
What is Python Function?
Python function is a group or block of related statements which are used to perform a particular task like computation, evaluation, etc. In python, function executes or runs only when they are called. Functions are use to break down the program into manageable and organised modular chunks.
Programmers generally use functions in python so that they don't have to write a code again and again and they can reuse codes whenever they want. Functions in python makes a program smaller and non repetitive. And as a result, function returns the data.
Syntax of Python Function;
Above, the function header starts with the followed by the names of function to uniquely identify it. Parameters or arguments are added through which a value passes to a particular function. Then, to mark the end of function header is used. In the next line, is used to tell what that function do. Statement ends up the body of function and it can be one or more than one valid statement. And it is optional but to the value from function, return statement is also added.
Docstring or Document string is the string after the function header in the body of function. Docstring generally tells us the functionality of a particular function or simply, we can say that it describes us what a function does. The use of Docstring is optional but it is considered as good programming practice.
"""Welcome to Answersjet and this is python Docstring example"""
#access doc stringprint(statement.__doc__)
Welcome to Answersjet and this is python Docstring example
The Return Statement
statement is used to end the body of function and to get the data as a result from the function. Return statement ends the execution of the function and give result from the function where the function is called.
The syntax of return statement includes the expression which will be evaluated and it returns the value to the called function.
Example of return statement:
a = 5
b = 15
c = a+b
return cprint("The sum of a and b is:",sum())
The sum of a and b is: 20
Types of Functions in Python
In python, there are two types of functions which are explained as follows;
Built-in functions are those functions which comes as a part with the python programming language such as, , etc.
User-defined functions are those functions created by the users to help them out by using keyword.
How to create Functions in Python
As we read above, user-defined functions are those functions which are created by users by using the keyword.
print("Welcome to Answersjet")
Calling Python Function
After creating a function, we can use that function in any program or another function just by calling that function by its name followed by the parenthesis with appropriate argument or parameter.
Python Calling Function Example:
print("Welcome to Answersjet")statement()
Welcome to Answersjet
Arguments of Python Functions
Arguments are type of information which a value passes to a particular function. And these arguments are specified inside the parentheses after the name of function. The programmers can add as many as arguments as they want, just separate them with the help of a comma.
Python Function with arguments Example:
if (x % 2 == 0):
Types of Arguments
There are 4 types of arguments through which we can pass the value to the function when it is called.
Required arguments are those arguments which are required to be passed to a function in a correct positional manner during the time of function call and if it fails to provide the argument or the argument position changes then it will result in an error.
Python Required Arguments Example:
message = "Hey! "+name
name = input("Please enter the name:")print(func(name))
Please enter the name: Deepak
Keyword arguments are those arguments which allows us to call a function and it passes the argument in a random manner. The argument names serve as keyword and identifies by the caller with the help of parameter names.
Python keyword Arguments Example:
def multiply(x, y):
print(multiply(x = 5, y = 2))print(multiply(y = 10, x = 7))
Default arguments are those arguments which gives a default value to an arguments in function call when no value is passed to that argument during the time of function calling.
Python Default Arguments Example:
print("My name is",name,"and my age is",age)me(name = "Deepak")
My name is Deepak and my age is 23
Variable-length arguments are those arguments which allows us to pass arguments in any number when we don't know the exact number of arguments. Variable-length arguments are defined using the as variable name at the function definition.
Python Variable-length Arguments Example:
total = 0
for no in numbers:
total = total + no
print("Sum is:", total)
addition(1, 5, 2, 5, 4)addition(7, 68, 2.5)
Sum is: 0
Sum is: 17Sum is: 77.5
From above we have learnt about the Python functions, types of python functions and arguments of functions. Python function is a group or block of related statements which are used to perform a particular task like computation, evaluation, etc. In python, there are two types of functions i.e., Built-in function and User-defined function. Arguments are type of information through which a value passes to a particular function.