Def function


I am new to the codecademy forum here is my question

I wanna ask what does def function really do
and how can I use it correctly?

what situation I need to use it.

Thanks for supporting and helps!


Function statements create functions and assigns names to them.
Functions are useful for giving a name to to some bigger operation, and for avoiding repeating the same code in multiple places.

Functions let you store behaviour for later use, useful for when something needs to be done from multiple places in your code or for simply putting a name to several steps. Functions also let you treat behaviour like data, you can for example put functions in a list or pass them as arguments to other functions

def add_5(n):
    return n + 5

def call(f, x):
    return f(x)

print call(add_5, 3)  # 8

Pointless, really, because add_5(3) would do the same thing. But there are more useful things you can do, like:

map(add_5, [1, 2, 3])  # [6, 7, 8]


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.