Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Can someone explain 'def' to me, how it works. Ty.

I do not really know how to use deff, and when, i would like somebody to write some good detailed example of 'def'.

1 Answer

Hi Mato,

The "def" keyword is used to define a function. It let's the Python interpreter know that the code that immediately follows "def" will be the name of a function that you want to define.

All code that is indented under the "def" statement will be part of that function

For example, here are two functions that are defined using the "def" keyword:

def my_function(argument1, argument2):
    print(argument1)
    print(argument2)

def new_function():
    print("This is a new function")

Other languages will have different keywords to define functions, but will work in a similar way. The syntax may be different but the principle is the same.

In JavaScript, for example, you'd use the word "function" instead of "def":

function myFunction() {
    alert("This is a function");
}

Hopefully this clears things up.