2. Give each group a different function printed on a piece of paper. The functions are listed below. Give students 5-10 minutes to discuss parameters and return values of their function. At this point in the course, the students know about printing, conditional statements, simple arithmetic operators, and functions.
Directions:
Your function is checkInput. Make sure you understand your
function since we'll be tracing through a program that
uses your function. All members of your group should understand the
function. Write C style comments for the
function checkInput.
int checkInput(int num){
if((num != 0) && (num != 1) && (num != 2)){
printf("The integer you entered
is not valid: %d\n", num);
return 0;
}
return 1;
}
What kind(s) of argument(s) does your function use?
What kind of value does your function return?
What happens when your function is called with the following arguments?
checkInput(-3)
checkInput(1)
checkInput(6)
Directions:
Your function is calculateFirstWord. Make sure you understand
your function since we'll be tracing through a program that uses your function.
All members of your group should understand the function. Write C style
comments for the function calculateFirstWord.
int calculateFirstWord (int num){
int temp;
if (num < -200){
num = -num;
}
temp = (num + 200) % 3;
return temp;
}
What kind(s) of argument(s) does your function use?
What kind of value does your function return?
Could you write the same function without using the variable temp?
If so, how?
What happens when your function is called with the following arguments?
calculateFirstWord(50)
calculateFirstWord(21)
calculateFirstWord(-202)
Directions:
Your function is firstWords. Make sure you understand your
function since we'll be tracing through a program that uses your function.
All members of your group should understand the function. Write C style
comments for the function firstWords.
int firstWords (int num){
int temp = calculateFirstWord(num); /* line A */
if (temp == 0){
printf ("Your friend ");
}
if (temp == 1){
printf ("Your instructor ");
}
if (temp == 2){
printf ("Today ");
}
if (temp > 2 || temp < 0){
printf ("Somebody ");
}
return temp;
}
What kind(s) of argument(s) does your function use?
What kind of value does your function return?
What happens at the line marked A?
Directions:
Your function is phrase. Make sure you understand your function
since we'll be tracing through a program that uses your function. All members
of your group should understand the function. Write C style comments for
the function phrase.
int phrase (int num){
num = num * num;
if(num <= 1){
printf("hopes that good fortune ");
}
else{
printf("is a good day ");
}
return num;
}
What kind(s) of argument(s) does your function use?
What kind of value does your function return?
What happens when your function is called with the following arguments?
phrase(5)
phrase(-6)
phrase(0)
Directions:
Your function is closingNum. Make sure you understand your
function since we'll be tracing through a program that uses your function.
All members of your group should understand the function. Write C style
comments for the function closingNum.
int closingNum(int num1, int num2){
if (num1 < 3){
return 0;
}
if (num2 == -5){
return -10;
}
return (205 / 10);
}
What kind(s) of argument(s) does your function use?
What kind of value does your function return?
What happens when your function is called with the following arguments?
closingNum(3, 0)
closingNum(4, -5)
closingNum(0, 126)
Directions:
Your function is closing. Make sure you understand your function
since we'll be tracing through a program that uses your function. All members
of your group should understand the function. Write C style comments for
the function closing.
void closing(int num){
int temp;
temp = closingNum(num, num+3); /* line B */
if(temp == 0 || temp == 1){
printf("and much knowledge will be with you
for");
}
else{
printf("to prepare for ");
}
}
What kind(s) of argument(s) does your function use?
What kind of value does your function return?
What happens at the line marked B?
3. After giving groups time to spend on the functions, the instructor will take on the role of main. Below is a copy of the function. The instructor traces the code. Upon a function call, the group with that function now takes on the role of explaining the execution. If a return value is passed back to the caller, have the group write down the value and give it to the group representing the caller. The instructor might want to have a single sheet of paper for groups to write down function parameters and return values.
int main(void){
/* declarations */
int valueByUser;
int temp;
int numRead;
/* prompt user for entering an integer -- 0,1,2 */
printf("Hello. Thank you for trying out the magic
message maker.\n");
printf("Please enter one of the following integers:
0, 1, 2\n");
/* store user input into valueByUser */
scanf("%d", &valueByUser);
/* check value entered by user */
if(checkInput(valueByUser) == 0){
printf("You entered an invalid integer:
%d\n", valueByUser);
return -1;
}
temp = firstWords(valueByUser);
temp = phrase(temp);
closing(temp);
printf("your assignment\n");
return 0;
}
Example of conversation by instructor:
I represent the execution of the function main. I have three
declared variables, all of type int. I print out the message "Hello.
Thank you for trying out the magic message maker. Please enter one
of the following integers: 0, 1, 2". The value entered by the
user is stored in the variable valueByUser. Let's say the user enters
0. I call the function checkInput with the parameter valueByUser.
In this instance, the value of valueByUser is 0. (Instructor writes
a 0 on an index card and asks the group with the checkInput function to
come up -- it's easiest to do this activity using an overhead projector
so the instructor can project the functions for the entire class.)
checkInput Group:
I receive one argument to my function. In this case, the value
of the argument is 0. I check the conditional statement. Num
is equal to 0, so the condition num != 0 is false, making the entire conditional
statement false. Thus, the value 1 is returned to the function who
called me -- main. (Group writes down a 1 on the index card and hands
it back to the instructor who represents main.)
This dialogue continues for the entire execution of main. At the end, a fortune will be printed:
User enters 0: Today is a good day to prepare for your assignment
User enters 1: Your friend hopes that good fortune and much knowledge
will be with you for your assignment
User enters 2: Your instructor hopes that good fortune and much
knowledge will be with you for your assignment