Python Functions
Python Functions
Python Functions is a block of statements that return the specific task. The idea is to put some
commonly or repeatedly done tasks together and make a function so that instead of writing the
same code again and again for different inputs, we can do the function calls to reuse code contained
in it over and over again.
Types of Functions
There are two types of function in Python programming:
Standard library functions - These are built-in functions in Python that are available to use.
User-defined functions - We can create our own functions based on our requirements.
Function Declaration
Calling a Function
def greet():
print('Hello World!')
# call the function
greet()
When the function is called, the control of the program goes to the function definition.
All codes inside the function are executed.
The control of the program jumps to the next statement after the function call.
Function Arguments
Arguments are specified after the function name, inside the parentheses. You can add as many
arguments as you want, just separate them with a comma.
The terms parameter and argument can be used for the same thing: information that are passed into
a function.
User-defined functions
No argument and no return value
No argument and a return value
With argument(s) and no return value
With argument(s) and a return value
def Adding():
a = 20
b = 30
Sum = a + b
print("After Calling :", Sum)
Adding()
def Multiplication():
a = 10
b = 25
Multi = a * b
return Multi
print("After Calling the Multiplication : ", Multiplication())
Multiplications(10, 20)
With argument(s) and a return value
Keyword Arguments
The idea is to allow the caller to specify the argument name with values so that the caller does not
need to remember the order of parameters.
# Keyword arguments
student(firstname='Geeks', lastname='Practice')
student(lastname='Practice', firstname='Geeks')
Output:
Positional Arguments
We used the Position argument during the function call so that the first argument (or value) is
assigned to name and the second argument (or value) is assigned to age. By changing the position, or
if you forget the order of the positions, the values can be used in the wrong places
def nameAge(name, age):
print("Hi, I am", name)
print("My age is ", age)
In Python Arbitrary Keyword Arguments, *args, and **kwargs can pass a variable number of
arguments to a function using special symbols. There are two special symbols:
# Driver code
myFun(first='ks', mid='for', last='ks')
Docstring
The first string after the function is called the Document string or Docstring in short. This is used to
describe the functionality of the function. The use of docstring in functions is optional but it is
considered a good practice.
The below syntax can be used to print out the docstring of a function.
Syntax: print(function_name.__doc__)
Anonymous Functions in Python
In Python, an anonymous function means that a function is without a name. As we already know the
def keyword is used to define the normal functions and the lambda keyword is used to create
anonymous functions.