Trouble writing functions

I understand what a function is and what it does. My biggest hurdle is knowing when to write a function and what code should go inside a function. It's almost equivalent to a writer's block for me.

I practice with some rudimentary mathematical problems, but expanding upon that is where I get stuck. I'm just seeking some advice or tips that will push me forward. Thanks.

2 comments · 81% upvoted · other discussions

comments

ace6807

Any time you have a group of code that could have reuse it should probably go into a function. If you are writing the same block of code more than once it should definitely go into a function. As for what should be in a function: you want to make sure your functions have only 1 job.

roflulz

Ideally, you can describe the full scope of the function in one sentence