stock market

What is a Function in Python? | Python Functions Explained with Examples

A function is a block of reusable code that performs a specific task . function help to organize code , improve readability and avoid repetition .

How to define Function in python

you define a function using the def keyword , parentheses () can hold parameters . this makes your code cleaner and easier to manage—especially when you need to repeat the same logic in multiple places.

How to call Function in python

Calling a function in Python is pretty straightforward! Once you’ve defined a function using the def keyword, you just need to write its name followed by parentheses. If the function takes arguments, you pass them inside the parentheses.

Types of function in python

  • function with no parameter
  • function with parameter
  • function with default parameters
  • function with variable arguments
  • function with keyword argument
  • lambda function
  • function with return value
  • recursive function

1. Function with no parameters

A function with no parameter in Python is one that doesn’t require any input when it’s called. It simply performs an action or returns a value without needing any external information.

2. Function with parameters

A function with parameters in Python is like a reusable block of code that expects certain information to be passed in when it’s called

3.function with default parameters

Functions can have default parameters, which means you can assign default values to arguments so that the function can be called with fewer arguments than it is defined to accept.

4.function with variable arguments

function with variable argument used when you don’t know how many arguments will be passed . In Python functions with different types of variable arguments .

These arguments take default values if no value is provided during the function call.

*args – Non-keyword variable arguments

  • Captures positional arguments.
  • Stored as a tuple.
  • You use it when you want to accept a variable number of positional values.

**kwargs – Keyword variable arguments

  • Captures keyword arguments .
  • Stored as a dictionary.
  • You use it when you want to accept a variable number of named values.

5.Lambda(anonymous) function

A lambda function in Python is a small, anonymous function defined using the lambda keyword. It’s typically used when you need a short function for a short period and don’t want to formally define it using def.

Syntax :

Example:

6.Lambda(anonymous) function

A function is a reusable block of code that performs a specific task. When a function has a return value, it means it sends back a result to the part of the program where it was called. This is done using the return statement.

Syntax:

Example

7.Recursive function

A recursive function that calls itself to solve smaller instances of a problem until it reaches a base case that stops the recursion.

Example

Leave a Reply

Your email address will not be published. Required fields are marked *

RkdigitalSchool