Introduction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

So far in this module, you’ve written snippets of code that perform tasks such as arithmetic, printing out song lyrics, making value comparisons, etc. Across all these snippets, you used the println() function to print the result.

This function took a primitive input, a String, as its argument and printed it on the console. In this lesson, you’ll dive deeper into functions and learn how to create functions to facilitate reusability in your code and avoid repetition. You’ll also learn how to call the functions, pass arguments, get return values etc.

In programming, a function is a block of code that encapsulates a piece of behavior that runs only when called and can be executed multiple times. This block of code has a few key features.

  • Simplicity - Isolating the related piece of logic in a program reduces its complexity and makes it much simpler to read.
  • Reusability - You make your logic reusable by extracting your logic into functions. The code is only written once but can be used in multiple places with different outputs.
  • Modularity - By breaking down your code into smaller functions, you also aid in the maintenance of your code and facilitate debug issues by narrowing down the scope.
See forum comments
Download course materials from Github
Previous: Quiz: Leverage Control Flows Next: Instruction