This is an introductory lab on functions in C using the following scenario:
Facebook has hired you to help write programs that interact with the user and his or her friends.
Your boss has asked you to create an age calculator to be used when a user registers for the
website.
This work is licensed under a Creative Commons Attribution 3.0 Unported License.
Original author: Halli Meth with Thomas Dvornik and David Janzen
Let's introduce a few basics about functions. This may be a good review if you recently learned about functions.
Vocabulary terms will be highlighted in bold and italics, then reviewed at the bottom.
Here is an example
function definition
:
double yardsToMeters(int yards)
{
return yards * 0.9144;
}
 "double" is the
return type
 "yardsToMeters" is the
function name
 "yards" is the
parameter
; "yards" is of
type
"int"
 the
function body
consists of everything inside the curly brackets
 Here is an example use of the function or
function call
:
yardsToMeters(4);
 This function call would return 3.6576
 When a function returns something, you need to use the value it returns. You could store the function's return value in a
variable
like this:

double answer = yardsToMeters(4);
 In the above example, the variable "answer" would be assigned the value of 3.6576
 Or, you could use the function's return value as the parameter in another function call, like this:

someFunctionThatAcceptsMeters(yardsToMeters(4))
 or

printf("4 yards is equivalent to %.3f meters.", yardsToMeters(4));
Vocabulary Review:
 A function definition has a return type, name, set of parameters enclosed in a set of parenthesis, and a body enclosed in a set of curly braces.
 The first line of a function definition (i.e. all but the function body) is sometimes called the
function signature
or
operation signature
.
 A function's return type and parameters can be any data type. The builtin or primitive data types that we will work with are: "short", "int", "long", "float", "double", "char"
 If a function does not have any parameters, you use the keyword "void" in place of a parameter list.
 If a function does not return anything, you use the keyword "void" as the return type. Functions that don't return anything are usually called
procedures
.
 A function name can be any word consisting of letters, digits, and _. Function names (and variable names) cannot begin with a digit.
 A function is only defined once, but it can be called many times.
 When you call a function, you pass in values to the parameters.
Suppose Facebook wants you to create an age calculator that will approximate age based on a given birth year
and the current year. Let's walk through creating a function to do this.
Based on this brief description, how many parameters will your function need?
regex
^\s*2\s*$
failedmessage
I'm sorry, that is not the right number of parameters.
successmessage
You got the right number of parameters.
parameters
What is the return type of your function?
regex
^\s*int\s*$
failedmessage
I'm sorry that is not the correct return type. Your return type should be one of the following: void, int, double, char
successmessage
That's the correct return type. Good Job!
returnType
For the purpose of this exercise we will name our function getApproxAge
and name our parameters birthYear and curYear in that order.
Finish the first line of the function, also known as the function header or signature.
Remember you need the return type, function name, and parameters. ( , )
regex
^\s*int\s*getApproxAge\s*\(\s*int\s*birthYear\s*,\s*int\s*curYear\s*\)\s*$
failedmessage
Check your answer. Be sure you have the return type, function name,
both parameters (in the correct order), and the parameter data types.
successmessage Good Job!
firstLine
Now let's write some examples for our function.
Enter values for the parameters, and corresponding expected return values to predict the output of our function.
Remember that we are just approximating age, so don't worry about what month it is and whether the birthday
has not yet occurred.
The first row of the table has been completed for you as an example. Fill in the remaining rows.
Birth Year
Current Year
Expected Output
1990
2010
20
equation
test1b  test1a
expected
test1c
failedmessage
I'm sorry, your 1st row of input is incorrect.
successmessage
test1a
test1b
test1c
equation
test2b  test2a
expected
test2c
failedmessage
I'm sorry, your 2nd row of input is incorrect.
successmessage
test2a
test2b
test2c
equation
test3b  test3a
expected
test3c
failedmessage
I'm sorry, your 3rd row of input is incorrect.
successmessage
Good Job!
test3a
test3b
test3c
Let's convert some of your examples into test cases with function calls.
We will use a special function named "checkit_int" that knows how to run tests for us.
This "checkit_int" function is already defined for you in a library called "checkit.h".
There are many forms of "checkit" for many different data types.
For instance, there is "checkit_double", "checkit_int", "checkit_char", and "checkit_string".
In general, you pass two values to "checkit_int": an expected value and an actual value.
For example, here are some tests that should pass:
checkit_int(6, 2*3)
checkit_char('c', 'c')
checkit_double(2.5, 5 / 2)
Here are some silly tests that should fail (you would never write tests like this because they don't make any sense):
checkit_int(7, 2*3)
checkit_char('c', 'd')
As you will learn later, computers handle floating point numbers in an interesting way. As a result, we normally
check to see if two double's are close rather than exactly equal. The question is "how close?"
The function checkit_double uses a delta (a positive number that indicates "how close" two numbers need to be to be considered equal). Here are some examples:
Now, let's create some tests with checkit_int for your getApproxAge function.
Test #
Expected Value
Actual Value
1
checkit_int(
20
,
getApproxAge(1990,2010)
)
2
checkit_int(
,
)
3
checkit_int(
,
)
4
checkit_int(
,
)
5
checkit_int(
,
)
functionCode
int getApproxAge(int birthYear, int curYear) { return curYear  birthYear; }
functionCall
@testActual2a
expectedOutput
@testExpected2a
smessage
Good job, your row 2 test passed!
fmessage
I'm sorry, your row 2 test failed.
testExpected2a
testActual2a
functionCode
int getApproxAge(int birthYear, int curYear) { return curYear  birthYear; }
functionCall
@testActual3a
expectedOutput
@testExpected3a
smessage
Good job, your row 3 test passed!
fmessage
I'm sorry, your row 3 test failed.
testExpected3a
testActual3a
functionCode
int getApproxAge(int birthYear, int curYear) { return curYear  birthYear; }
functionCall
@testActual4a
expectedOutput
@testExpected4a
smessage
Good job, your row 4 test passed!
fmessage
I'm sorry, your row 4 test failed.
testExpected4a
testActual4a
functionCode
int getApproxAge(int birthYear, int curYear) { return curYear  birthYear; }
functionCall
@testActual5a
expectedOutput
@testExpected5a
smessage
Good job, your row 5 test passed!
fmessage
I'm sorry, your row 5 test failed.
testExpected5a
testActual5a
Now finish the body of our function.
The body should calculate the approximate age, based on our two parameters.
Try to do it in a single return statement.
int getApproxAge(int birthYear, int curYear)
{
}
functionCode
@functionbody
functionCall
getApproxAge(1990,2010)
expectedOutput
20
smessage
Good job, your code passed a test!
fmessage
I'm sorry, your failed a test.
functionbody
functionCode
@functionbody
functionCall
getApproxAge(1990,2015)
expectedOutput
25
smessage
Good job, your code passed a test!
fmessage
I'm sorry, your failed a test.
functionbody
functionCode
@functionbody
functionCall
getApproxAge(2006,2008)
expectedOutput
2
smessage
Good job, your code passed a test!
fmessage
I'm sorry, your failed a test.
functionbody
Great job! As you probably noticed, our age approximating function isn't very accurate because it only uses years.
What if someone was born in December, 1990, but it is currently November 2010? Our age approximator says they are 20,
but they are really only 19 years old.
Let's make a better approximation of someone's age by using both the year and the month (don't worry, we will add in the day later).
For the purpose of this exercise we will name this new function getApproxAge2.
Your return type is still an integer, but how many parameters will your new function need?
regex
^\s*4\s*$
failedmessage
I'm sorry that is not the right number of parameters. Remember we are using a year and month for both the current year and the birth year.
successmessage
Good Job!
parameters2
Let's write some tests for our function.
The first row has been filled out for you as an example.
Birth Year
Current Year
Birth Month
Current Month
Expected Output
1990
2010
6
7
20
functionCode
int ageCalculator(int bYear, int cYear, int bMonth, int cMonth) { if(bMonth <= cMonth) { return cYearbYear; } else { return (cYearbYear)  1; } }
functionCall
ageCalculator(@test4a, @test4b, @test4c, @test4d)
expectedOutput
@test4e
smessage
fmessage
I'm sorry, the 2nd row is incorrect.
test4a
test4b
test4c
test4d
test4e
functionCode
int ageCalculator(int bYear, int cYear, int bMonth, int cMonth) { if(bMonth <= cMonth) { return cYearbYear; } else { return (cYearbYear)  1; } }
functionCall
ageCalculator(@test5a, @test5b, @test5c, @test5d)
expectedOutput
@test5e
smessage
fmessage
I'm sorry, the 3rd row is incorrect.
test5a
test5b
test5c
test5d
test5e
functionCode
int ageCalculator(int bYear, int cYear, int bMonth, int cMonth) { if(bMonth <= cMonth) { return cYearbYear; } else { return (cYearbYear)  1; } }
functionCall
ageCalculator(@test6a, @test6b, @test6c, @test6d)
expectedOutput
@test6e
smessage
Good Job!
fmessage
I'm sorry, the 4th row is incorrect.
test6a
test6b
test6c
test6d
test6e
Now write the function getApproxAge2 to calculate an approximate age using the birth and current months and years.
Refer to the parameters as birthYear, curYear, birthMonth and curMonth in that order.
functionCode
@betterAge
functionCall
getApproxAge2(1980, 2010, 6, 1)
expectedOutput
29
smessage
Good Job!
fmessage
I'm sorry, your better age calculator does not work properly. Please review the specifications.
betterAge
We will now write a function that will calculate the exact age of the person.
Since we are now including the birth day and current day...
How many parameters will your function need?
regex
^\s*6\s*$
failedmessage
I'm sorry that is not the right number of parameters.
successmessage
Good Job!
parameters4
First let's write some tests for our function.
Complete the following table.
Note that some of the entries have been completed or partially completed for you.
Birth Year
Current Year
Birth Month
Current Month
Birth Day
Current Day
Expected Output (Age)
1990
2010
6
6
2
3
20
1985
2010
5
5
3
1
regex
^\s*24\s*$
failedmessage
I'm sorry, the expect output you entered for row 2 is incorrect.
successmessage
test7
functionCode
int getAge(int birthYear, int curYear, int birthMonth, int curMonth, int birthDay, int curDay) { int current = curYear*10000 + curMonth*100 + curDay; int birth = birthYear*10000 + birthMonth*100 + birthDay; return (current  birth)/10000; }
functionCall
getAge(@test8a, @test8b, @test8c, @test8d, @test8e, @test8f)
expectedOutput
@test8g
smessage
fmessage
I'm sorry, the 3rd row of the table is incorrect.
test8a
test8b
test8c
test8d
test8e
test8f
test8g
functionCode
int getAge(int birthYear, int curYear, int birthMonth, int curMonth, int birthDay, int curDay) { int current = curYear*10000 + curMonth*100 + curDay; int birth = birthYear*10000 + birthMonth*100 + birthDay; return (current  birth)/10000; }
functionCall
getAge(@test9a, @test9b, @test9c, @test9d, @test9e, @test9f)
expectedOutput
@test9g
smessage
fmessage
I'm sorry, the 4th row of the table is incorrect.
test9a
test9b
test9c
test9d
test9e
test9f
test9g
functionCode
int getAge(int birthYear, int curYear, int birthMonth, int curMonth, int birthDay, int curDay) { int current = curYear*10000 + curMonth*100 + curDay; int birth = birthYear*10000 + birthMonth*100 + birthDay; return (current  birth)/10000; }
functionCall
getAge(@test10a, @test10b, @test10c, @test10d, @test10e, @test10f)
expectedOutput
@test10g
smessage
fmessage
I'm sorry, the 5th row of the table is incorrect.
test10a
test10b
test10c
test10d
test10e
test10f
test10g
functionCode
int getAge(int birthYear, int curYear, int birthMonth, int curMonth, int birthDay, int curDay) { int current = curYear*10000 + curMonth*100 + curDay; int birth = birthYear*10000 + birthMonth*100 + birthDay; return (current  birth)/10000; }
functionCall
getAge(@test11a, @test11b, @test11c, @test11d, @test11e, @test11f)
expectedOutput
@test11g
smessage
Good Job!
fmessage
I'm sorry, the 6th row of the table is incorrect.
test11a
test11b
test11c
test11d
test11e
test11f
test11g
Now write the function to calculate the exact age of a person when given their
birth year, month, and date, as well as the current year, month, and date.
You can use this math trick to calculate age!
1. Convert inputed values into this format: YYYYMMDD
2. Subtract the birth information from the current information
3. The first three digits of your answer is the person's age.
(Note that in most cases the first digit will be zero)

Example:
 Birth: 19900510
 Current: 20100803
 Current  Birth : 0200293
 First 3 Digits: 020
 This person is 20 years old.
For the purposes of this exercise please name your function ageCalculator and
refer to your parameters as birthYear, curYear, birthMonth, curMonth,
birthDay, curDay in that order.
functionCode
@ageCalculator
functionCall
ageCalculator(1980, 2010, 6, 6, 2, 1)
expectedOutput
29
smessage
Good Job!
fmessage
Your age calculator function compiles, but it does not work properly. Please review the specifications.
ageCalculator
When you are done with this step, complete this survey.
You will be asked to
copy your program (function and tests) from this problem into the last question of the survey.
Use the C programming area on the first page of WebIDE to
complete the following program. Write a function and a set of tests to solve the following problem:
Employees are paid time and a half for overtime.
Overtime is any hours above 40 in a week.
Write a function that accepts a total number of hours worked in a week (e.g. 45),
and an hourly pay rate (e.g. 11.50).
Your function should return the
employee's total pay for the week, rounded down to the nearest dollar.
You may assume that both parameters will be positive.
Be sure that your tests thoroughly test your function.
Congratulations! You made it to the end.