Python
  • Getting Started
    • Input and Output
    • Variables
    • Data Types
    • Conditional Structures
      • Humidity Level
      • Financial Transactions
      • Fruit-Vending Machine
      • Magic While
      • FizzBuzz
    • Functions
      • Hashtag Generator
  • Data Structures
    • Lists
    • Dictionary
    • Set
    • Interpreter Expressions
  • OOPs
    • Classes and Objects
    • Exception Handling
      • Simple Calculator
  • System Programming
    • File Operations
    • Directory Navigation
    • Process Creation
    • Threads
Powered by GitBook
On this page
  1. Getting Started

Functions

I LOVE DOING THIS :)

A function is a block of code that takes in some data and, either performs some kind of transformation and returns the transformed data, or performs some task on the data, or both

How to create and call a Function

>>> def add(num1, num2):
...    result = num1 + num2
...    return result
...
>>> add(10,10)
20

Keyword def: This is the keyword used to say that a function will be defined now, and the next word that is there, is the function name

Note that arguments 10 and 10 have been passed. Hence, the return value will be 20. You can put any two numbers in place of 10 and 10, and it will return the corresponding sum of the two numbers.

>>> def add(num1, num2):
...    result = num1 + num2
...    return result
...
>>> sum = add(30,30)
>>> print sum
60
PreviousFizzBuzzNextHashtag Generator

Last updated 2 years ago