Learn

As you continue to write more code there will be times when you’ll need to reuse a set of instructions. You might be tempted to reuse the code by copying and pasting it, but in times like this, a function is a great tool to use.

A function is a group of instructions that is given a name to be used elsewhere in the code, often repeatedly. Functions can take in a set of inputs and produce some output.

Cookie ingredients are input into a function, make-cookie. The output of the function is a cookie.

In the photo above we use the idea of a function to make cookies. We collect a set of instructions (the recipe) under the name makeCookie. Baking a cookie requires a set of inputs (the ingredients). The instructions inside the function act on the input ingredients to produce the output (a cookie!).

Functions are great because:

  • You can write DRY (Don’t Repeat Yourself) code.
  • You can execute any number of instructions with a single line of code.
  • Once a function exists, you only need to worry about the inputs and output and not about the instructions inside.

In C, you have already worked with one function. The main() function is the entry point to any C program and you will understand it better as you go through this lesson.

Instructions

Look at the main() function in the script.c file. There are 2 lines, one that calls a function abs() and then the printf() function to output the value in absValue.

Run the program to see the output think about all the things happening in the code that we can’t see. This is a good example of DRY (Don’t Repeat Yourself) coding in the C programming language.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?