Programming in C++ (Part 5 Functions)
Tags: cpp, beginners
Published: 2021-04-14 23:02:54

This group of playbacks describes another flow of control altering mechanism called functions. A function is a named block of code that can be *called* and the flow of control will jump to it. 

When calling a function some data can be passed into it (called parameters) and the function can return a piece of data when it is complete (called a return value). I discuss passing in data 'by value' versus 'by reference'. I show that every variable has a limited lifetime that it sits in memory, or scope.

* [Functions](https://markm208.github.io/cppbook/chapter5/01/index.html)
* [Value returning functions](https://markm208.github.io/cppbook/chapter5/02/index.html)
* [Functions with parameters](https://markm208.github.io/cppbook/chapter5/03/index.html)
* [Passing parameters by reference](https://markm208.github.io/cppbook/chapter5/04/index.html)
* [The scope of variables](https://markm208.github.io/cppbook/chapter5/05/index.html)
* [Prime number function](https://markm208.github.io/cppbook/chapter5/06/index.html)
* [Passing arrays to functions](https://markm208.github.io/cppbook/chapter5/07/index.html)

###Call to Action
Now that you have reviewed the guided code walk-throughs I ask that you write a few programs:

####Problem 1
Problem 1 asks you to extend the prime number problem from a previous post. Write a program that includes a function that will print all the prime numbers in a range. The program will ask for a lower bound and an upper bound and print all the primes in that range.

####Problem 2
Problem 2 asks you to write a function that takes an integer year number and returns a `bool` whether that year is a leap year or not. The calculation for leap year is as follows:
* most years evenly divisible by four are leap years
* if a year is divisible by four and is a century year, like 1800 or 1900, then it is NOT a leap year
* if a century year also happens to be divisible by 400, like 2000 or 2400, then it is a leap year
 
The function should look something like this:
```
bool isLeapYear(int year)
{
    //calculate if it is leap year or not and return true or false
}
```

####Problem 3
Problem 3 asks you to write a program that will print a calendar for a whole year given the day that January 1st falls on. Your program will prompt for the day that January 1st falls on and the year. You must print the calendar for all 12 months of that year. The options for the first day of the year will be entered with the first three letters of each of the seven days of the week from Sunday ('sun') to Saturday ('sat').

The year does not need to be validated, but the day of the week does. You should not allow the user to enter in a value other than 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', or 'sat'.

The format of your calendar should be very similar to the example below. You must use at least one function in addition to the main function. Pass data between functions, do not use global variables.

The final requirement is that your program should only display one month at a time and then wait for some user input before continuing.

It is very important that you come up with a plan to solve this program before you begin coding (as it is with every program). Think about how you would print a single month's calendar given the day of the month that it begins on.

Sample Output:
```
What year do you want the calendar for?
2003

What day of the week does January 1st fall on (sun for Sunday, mon for Monday, etc..)?
s
Invalid Entry- please enter the first three letters of the day
 
What day of the week does January 1st fall on (sun for Sunday, mon for Monday, etc..)?
wed

    January 2003
 S  M  T  W  T  F  S
---------------------
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Do you want to continue? y

   February 2003
 S  M  T  W  T  F  S
---------------------
                   1
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28

Do you want to continue? y
...
...
```

###Comments and Feedback
You can find all of these code playbacks in my free 'book', [An Animated Introduction to Programming in C++](https://markm208.github.io/cppbook/). I am always looking for feedback so please feel free to comment here or to send me a message. You can follow me on twitter [@markm208](https://twitter.com/markm208).