Share via:


▪️ A function is a block of code that performs a specific task.
▪️ Functions are defined by using the def keyword which follows by function name and parentheses which may have parameters.
▪️ Any function can accept zero or more parameters.
▪️ The body of the function contains the code that performs a specific task.
▪️ Function returns a value using the return keyword, this makes a function to pass data back to the code from where it is called.
▪️ To execute any function, you need to call it by the function name followed by parentheses.

Types of functions:
In python there are different types of functions, now you will see some of them.
1. Built-in functions
2. User defined functions
3. Lambda functions (Anonymous functions)


Built-in Functions:
Functions whose functionality is predefined. These are stored in the interpreter and come into action when you call them.
These can be accessed from any part of the code.
Examples: print ( ), len ( ), max ( ), set ( ).


User defined functions:
Functions that are defined by the user to perform a specific task.
Users can define their own function using the def keyword followed by function name.


Lambda functions:
Lambda functions are small, anonymous functions defined using the lambda keyword.
They can have any number of arguments but can only contain a single expression.


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.
There are several ways to pass arguments to a function, each providing different capabilities and flexibility.
Positional arguments:
The values passed as arguments are assigned to parameters in the function definition in the same order.
It is the most common way of passing arguments to functions in Python.

Keyword Arguments:
Keyword arguments are passed with their corresponding parameter names, allowing for more flexibility in the order of arguments.
using the format parameter_name=value in the function call.

Variable length positional arguments
It allows a function to take any number of positional arguments.
If the exact number of arguments are unknown then arbitrary arguments can be used, it can be done by adding “*” before the argument makes it an arbitrary argument.
This way the function will receive a tuple of arguments, and can access the items accordingly:


return keyword is used to return the values from a function.

When returned from a function it has to be collected in the variable and that variable can be used in the above example it’s area.
1. No return, No arguments
2. No return, with arguments
3. Returns value, No arguments.
4. Return value, with arguments.
1. No Return, No arguments
A function with no parameters and no return value, a simple function that performs a specific task without taking any input parameters and without returning any result.
For example, consider the function “greet( )” which is used to print “Hello”, it does not return anything and does not take any arguments.
Example :

2. No Return, With Arguments
A function with parameters but no return value, it accepts input data through parameters but does not explicitly return any result.
For example, consider a function “greet()” used to greet a person by a name which is taken as input.
Example :

3. Returns Value, No Arguments
A function that returns a value but takes no arguments takes after the execution of the function.
For example, consider a function “get_random_number()” that takes no arguments and returns a random value between 1 to 100.

4. Returns Value, With Arguments
A  function that returns a value and also accepts arguments, it is a common type of function.
For example, Consider a function “power()” used to return  the power of the number.


Author    : Venkat Vinod Kumar Siram 
LinkedIn :
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates.
KTExperts is always active on social media platforms.
Facebook  :
LinkedIn    :
Twitter       :
YouTube   :
Instagram  :
Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Add Comment