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

Last updated