70 likes | 177 Views
CS1010E Tutorial 2 Q5. Using function,. // This is Q5. // Include preprocessing directive. #include < stdio.h > int integerAdd ( int x, int y); //Function Prototype for part A. double doubleAdd (double x, double y); //Function Prototype for part B.
E N D
CS1010E Tutorial 2 Q5. Using function, // This is Q5. // Include preprocessing directive. #include <stdio.h> intintegerAdd (int x, int y); //Function Prototype for part A. double doubleAdd (double x, double y); //Function Prototype for part B. // This programme prints the answer to q5a and q5b. int main (void) { // Declare variable used, where result1 is part1 and result2 is part2. int result1; double result2;
CS1010E Tutorial 2 Q5. //Store the computation in different data type compared to the function. result1 = doubleAdd (4.7,5.5); result2 = integerAdd (4.75,5.25); // Display the end results from the data type conversion. printf("result1 = %d\nresult2 = %lf\n", result1, result2); return 0; } intintegerAdd (int x, int y) { return x+y; } double doubleAdd (double x, double y) { return x+y; } • result1 = 10 • result2 = 9.000000
CS1010E Tutorial 2 Q5. CS1010E Tutorial 2 Q5. Using tracking (part a), main doubleAdd (double x, double y) : : int result; result = doubleAdd (4.7,5.5); ( result ) : : return x+y; (x ) (y ) ( return ) 4.7 5.5 10 10.2
CS1010E Tutorial 2 Q5. Using tracking (part b), main integerAdd (int x, int y) : : double result; result = integerAdd (4.75,5.25); ( result ) : : return x+y; (x ) (y ) ( return ) 4 5 9 9.000000
CS1010E Tutorial 2 Q5. • Key pointers • Data type conversion will occur automatically when argument input parameter (i.e when 4.75 4 in part b) • Data type conversion will occur automatically when return value assignment statement (i.e when 10.2 10 in part a) • Precision of result may decreases during the conversion.
CS1010E Tutorial 2 Q5. • Additional pointer • No conflict in variable names from function to function.
CS1010E Tutorial 2 Q5. Using function, // This is Q5. // Include preprocessing directive. #include <stdio.h> intintegerAdd (intx, inty); //Function Prototype for part A. double doubleAdd (double x, double y); //Function Prototype for part B. // This programme prints the answer to q5a and q5b. int main (void) { // Declare variable used, where result1 is part1 and result2 is part2. int result1; double result2;