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 Python Basics (2015) Logic in Python Fully Functional

Mariya Shklyar
Mariya Shklyar
1,022 Points

How to practice functions?

I want to practice writing functions. Where can I find some examples with answers? It seems that I understand the video, but when I want to practice in workspace, I don't have anyidea how to begin. That means that I don't undestand the concept. But I don't even know what I don't understand.

1 Answer

Hi Mariya

In programming you should always follow the rule DRY or don't repeat yourself. That's where functions come in. Imagine your trying to write code that adds 2 numbers. See below

# someone asks you to write code to add 2 numbers 10 and 20 and print it. You do the following below.
a = 10
b= 20
print(a+b)

# another person then asks you to do the same but says, i now need you to add 30 and 50.You do the following below.

a = 30
b= 50
print(a+b)

# as you can see you are now repeating code.

# On the other hand if you make a function called add that takes to arguments it would be more efficient. See below

def add(num1,num2):
    print(num1 + num2)

# now when you need to add two numbers all you need to do is call the add function.

add(10,20)
add(30,50)

you can also practice writing functions at http://codingbat.com/python

hope this helps