/* CSC15 lab4: Functions Due Wednesday 3/7/01 FIRST, RECITE THE FOLLOWING TO YOURSELF 100 TIMES: CALLS TO FUNCTIONS THAT DON'T RETURN VALUES ARE STATEMENTS; CALLS TO FUNCTIONS THAT RETURN VALUES ARE EXPRESSIONS. CALLS TO FUNCTIONS THAT DON'T RETURN VALUES ARE STATEMENTS; CALLS TO FUNCTIONS THAT RETURN VALUES ARE EXPRESSIONS. ... For this lab and assignment you are to write a number of functions and use them in a program. The following code contains some sample functions and explanations. PLEASE READ THEM AND MAKE SURE YOU UNDERSTAND EVERYTHING BEFORE WRITING YOUR OWN FUNCTIONS! */ /* This program illustrates diffferent styles of functions: */ #include #include // f1 is a function that doesn't return any value. // These functions are called as STATEMENTS: void f1(int x) { if (x > 10) cout << "number is big\n"; else cout << "number is small\n"; } // to use the above function, execute a statement such as "f1(9);" // remember that x is a local variable within f1. // The following function computes a simple mathematical formula: (x*x)+1 // It returns the result of the computation - that is, the function returns // an integer. Therefore, calls to this function are to be used as // EXPRESSIONS. For example 3+f2(2) is the same as 3+5, or 8. int f2(int x) { int answer; answer = (x*x) + 1; return answer; // type of value returned must match declaration (int) // the entire body can also be replaced by return (x*x)+1; } // MAKE SURE // YOU UNDERSTAND: f2(3) is the same as 10, so "f2(3);" doesn't make sense // as a statement - it'd be like 10; // The following function is an alternative to function f1 in that it returns // a string instead of printing it out. string f1b(int x) { if (x > 10) return "number is big\n"; else return "number is small\n"; } // A function that calls another function: int f3() { int z; cin >> z; // get input from keyboard return f2(z); // functions can also call other functions (already defined) } // in the above, since f2 returns an integer, f2(z) can be used in place of // any integer expression, so "return f2(z);" makes sense, just as "return 3;" // does. // main is the "entry point" function int main() { f1(4); // why is this a statement? cout << f2(3) << "\n"; // should print 10 cout << f1b(3) << "\n"; // why an expression? cout << f3() << "\n"; // should print what is typed in squared +1 return main(); // a function can even call itself! What do you think // will happen? } /* Using the above examples as guide, you are to write the following functions: 0. Write a function that returns no value (void), that prints out 'a', 'b' and 'c' on separate lines. This function doesn't have any parameters (because it already knows what to print) I'll even start it off: void printabc() { // your code } 1. Now write a function print3 that takes three parameters, all characters, and prints them out on separate lines. That is, a statement of the form print3('f','g','h') should print f, g and h on separate lines. I'll start it off again: void print3(char c1, char c2, char c3) { // your code } 2. Ok, that was baby stuff. Now, write a function absval1 that takes an integer as its parameter, and prints out its absolute value (need to multiply parameter by -1 if it's negative to get the absolute value). This function takes one parameters, and again doesn't return any value (void): 3. Now let's write a function that returns a value. Write absval2, which, instead of printing out the absolute value, returns it. Now, make sure you understand that a call to this function such as absval(-3) is not a statement but an expression, so "absval(-3);" doesn't make sense. You have to use it as an integer expression as in "x = absval(-3);" 4. Write a function that takes three arguments: int alu(int x, int y, char action) The third argument is a character that should be 'a' 's', 'm' or 'd', representing addition, subtraction, multiplication, and division. Depending on this character, you are to perform the requested action on x and y and return the computed value. (you'll need if-else again). If the action character is not one of the four above, this function should return 0. for example, alu(3,4,'a') should return 7, alu(3,4,'m') should return 12. 5. Write a function void test() that doesn't take or return any value, but which calls the four functions above and display their results. The test function should take input values from the user. Make sure that the output is readable, such as Enter a value: -8 The absolute value of -8 is 8, etc... You should call the alu funciton 4 times. 4. Your main function should basically just call the test function: // the entry point of the program: int main() { char c; // a statement is needed here to call the test function. cout << "\n run program again? "; cin >> c; if (c == 'y') { return main(); } // what does this do? else { return 0; } } COMMENTING GUIDELINES: Before you write a function, you should first explain its purpose and how it's to be used. For example: // the following function takes two integer values, and depending on the // third parameter, will perform an arithmetic action on the values and // return the result. The valid options for the third value are // 'a', 's', 'm' and 'd' for add, subtract, multiply and divide respectively. int alu(int x, int y, char action) Now people will know how to call the function. Remember that a function is a self-contained program, so you also need to comment the body of the function. I.e, every variable should be explained. If-else statements should be justified. Other comments may also be necessary to clarify the structure of your program. WHAT TO HAND IN 1. Paper write-up of the ALU function. 2. Source code, well commented (comments will be worth ~20%) 3. Sample output (cut and paste output into a file with a ".txt" extension and type print204 filename.txt). */