<commit_before>##### Module 2
[Return to Prep for Class 1](../../class1-prep)

# Resource: Calling Functions

Let’s review how to use functions!  Over the course of your career as a programmer, you’ll use many, many functions, both those others have written, and those you’ve written yourself. As we’ve seen before, we can run a library function like so:

```
#include <cs50.h>
string exampleString = “hello!”;  // set up an example variable to use

strlen(exampleString);
```

`strlen` takes a string, and calculates the length of that string, i.e. how many characters it contains.

But wait!  Something is missing here; we got the length of the string, but we didn’t do anything with it!  If we, say, wanted to print the length of the string, how could we do this?

## Return Values

`strlen` is an example of a function that "gives back" a value.  We call this a return value.  To use the value that a function hands back to us, we can store it in a variable:

```
string exampleString = “hello!”;

// call strlen and store its return value in a variable
int exampleStringLength = strlen(exampleString);

printf(“The string has %i characters!\n”, exampleStringLength);
```

## Side Effects

`strlen` takes a `string` and returns an `int`. The value of `strlen("hello")` is `5`, because  But some functions actually don't return any value at all. For example, `printf`. The `printf` function does not compute a value. Instead, printf *does* something. What is the "value" of `printf("hello")`? Nothing at all, but something has "happened" after we call it: namely the user now sees "hello" on their console.

We call these interactions, either changes to the input or the outside world, "side effects".

A function like `strlen`, which has a return value, but does not alter the input or the outside world, does not have side effects.

Why are side effects important?  Functions with side effects are common and useful, but can create one more place to look when tracking down a bug.  Since they change things around when they run, they’re one more thing to pay attention to when you’re troubleshooting a program.  Pay attention to if questions have side effects or not!  When you write large programs with your own functions, knowing whether a side effect can help you track down bugs.<commit_msg>finished sentence on why strlen returned 5<commit_after>##### Module 2
[Return to Prep for Class 1](../../class1-prep)

# Resource: Calling Functions

Let’s review how to use functions!  Over the course of your career as a programmer, you’ll use many, many functions, both those others have written, and those you’ve written yourself. As we’ve seen before, we can run a library function like so:

```
#include <cs50.h>
string exampleString = “hello!”;  // set up an example variable to use

strlen(exampleString);
```

`strlen` takes a string, and calculates the length of that string, i.e. how many characters it contains.

But wait!  Something is missing here; we got the length of the string, but we didn’t do anything with it!  If we, say, wanted to print the length of the string, how could we do this?

## Return Values

`strlen` is an example of a function that "gives back" a value.  We call this a return value.  To use the value that a function hands back to us, we can store it in a variable:

```
string exampleString = “hello!”;

// call strlen and store its return value in a variable
int exampleStringLength = strlen(exampleString);

printf(“The string has %i characters!\n”, exampleStringLength);
```

## Side Effects

`strlen` takes a `string` and returns an `int`. The value of `strlen("hello")` is `5`, because its input argument has 5 letters.

But some functions actually don't return any value at all. For example, `printf`. The `printf` function does not compute a value. Instead, printf *does* something. What is the "value" of `printf("hello")`? Nothing at all, but something has "happened" after we call it: namely the user now sees "hello" on their console.

We call these interactions, either changes to the input or the outside world, "side effects".

A function like `strlen`, which has a return value, but does not alter the input or the outside world, does not have side effects.

Why are side effects important?  Functions with side effects are common and useful, but can create one more place to look when tracking down a bug.  Since they change things around when they run, they’re one more thing to pay attention to when you’re troubleshooting a program.  Pay attention to if questions have side effects or not!  When you write large programs with your own functions, knowing whether a side effect can help you track down bugs.
{'fraction_non_alphanumeric': 0.07441561226678105, 'fraction_numerical': 0.0030023589963542783, 'mean_word_length': 4.312072892938496, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7664643', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1195, 'n_words': 739}