FUNCTIONS IN PYTHON

FUNCTIONS IN PYTHON

Function in Python is reusable code that performs a specific task.

  • It divides programs into smaller parts, making the code easier to read and understand.
  • Functions can accept inputs called parameters or arguments.
  • A function is a set of instructions that gets executed whenever it is called.
  • Functions can return a value using the return keyword. If no return keyword is specified, the function implicitly returns None.
  • Functions make programs more organized, readable, and maintainable
  • To call a function use the function name followed by its parenthsis.

Functions are of three types.

  1. Built-in
  2. User-defined
  3. Lambda

Built-in :

  • These are predefined functions already available in Python.
  • They do not require importing any additional libraries or modules.
  • Built-in functions perform common tasks like mathematical operations and type conversions.
  • Examples: print(), int(), len(), etc.

User-defined Functions in Python

  • User-defined functions are functions created by the user to perform specific tasks as per their requirements.
  • They are defined using the def keyword.
  • A function can return a value using the return keyword.
  • User-defined functions support complex and logical code.
  • Functions are called by their name, followed by parentheses.
  • They can be reused, making programs more organized and manageable.
  • The number of lines in a user-defined function depends on how simple or complex the task is.

Lambda Function

  • A lambda function is an anonymous function.
  • It is defined using the lambda keyword.
  • Used for short, single-line expressions.
  • It can only have one expression.
  • Lambda functions can take multiple arguments.
  • No return keyword is needed.
  • Allows you to pass functions as arguments.
  • Lambda functions are useful when you need a function for a short period or a single line of code

Why Do We Use Functions ???

  • Reusability: You can use the same code multiple times without rewriting it.
  • Modularity: Functions break programs into smaller, easier-to-handle parts.
  • Readability: They make the code cleaner and easier to understand.
  • Debugging: Errors are easier to find and fix within a function.
  • Efficiency: Functions save time by reducing repeated code.
  • Flexibility: You can use inputs (parameters) to make functions work for different tasks.
  • Maintainability: Changes can be made in one place instead of updating code everywhere.



To view or add a comment, sign in

More articles by Rithika Shetty

  • ENSEMBLE LEARNING :

    Ensemble learning is a machine learning technique that combines multiple models to improve overall performance…

  • Machine learning

    Unlocking the Power of Machine Learning! ✨ Machine Learning is an exciting branch of computer science that empowers…

Insights from the community

Others also viewed

Explore topics