- Code reuse is a very important part of programming in any language.
- Increasing code size makes it harder to maintain.
- For a large programming project to be successful, it is essential to abide by the Don't Repeat Yourself, or DRY, principle.
FACT : Bad, repetitive code is said to abide by the WET principle, which stands for Write Everything Twice, or We Enjoy Typing.
- A function is a block of code which only runs when it is called.
- You can pass data, known as parameters, into a function.
- A function can return data as a result.
- Any statement that consists of a word followed by information in parentheses is a function call.
Here are some examples that you've already seen:
print("Hello world!")
range(2, 20)
str(12)
range(10, 20, 3)
NOTE : The words in front of the parentheses are function names, and the comma-separated values inside the parentheses are function arguments.
- In addition to using pre-defined functions, you can create your own functions by using the def statement.
- Here is an example of a function named my_func.
It takes no arguments, and prints "spam" three times. It is defined, and then called.
The statements in the function are executed only when the function is called.
def my_func():
print("spam")
print("spam")
print("spam")
my_func()
>>>
spam
spam
spam
>>>
NOTE : The code block within every function starts with a colon (:) and is indented.
- You must define functions before they are called, in the same way that you must assign variables before using them.
hello()
def hello():
print("Hello world!")
>>>
NameError: name 'hello' is not defined
>>>
- All the function definitions we've looked at so far have been functions of zero arguments, which are called with empty parentheses.
- However, most functions take arguments.
- The argument is defined inside the parentheses.
- The example below defines a function that takes one argument:
def print_with_exclamation(word):
print(word + "!")
print_with_exclamation("spam")
print_with_exclamation("eggs")
print_with_exclamation("python")
>>>
spam!
eggs!
python!
>>>
- You can also define functions with more than one argument; separate them with commas.
def print_sum_twice(x, y):
print(x + y)
print(x + y)
print_sum_twice(5, 8)
>>>
13
13
>>>
- Function arguments can be used as variables inside the function definition.
- However, they cannot be referenced outside of the function's definition.
- This also applies to other variables created inside a function.
def function(variable):
variable += 1
print(variable)
function(7)
print(variable)
>>>
8
NameError: name 'variable' is not defined
>>>
NOTE : Technically, parameters are the variables in a function definition, and arguments are the values put into parameters when functions are called.
- Although they are created differently from normal variables, functions are just like any other kind of value.
- They can be assigned and reassigned to variables, and later referenced by those names.
def multiply(x, y):
return x * y
a = 4
b = 7
operation = multiply
print(operation(a, b))
>>>
28
>>>
- Functions can also be used as arguments of other functions.
def add(x, y):
return x + y
def do_twice(func, x, y):
return func(func(x, y), func(x, y))
a = 5
b = 10
print(do_twice(add, a, b))
>>>
30
>>>
HINT : As you can see, the function do_twice takes a function as its argument and calls it in its body.
https://www.w3schools.com/python/python_functions.asp
https://www.tutorialspoint.com/python/python_functions.htm
https://www.programiz.com/python-programming/function