1 / 7

CS1010E Tutorial 2 Q5.

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.

Download Presentation

CS1010E Tutorial 2 Q5.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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;

  2. 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

  3. 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

  4. 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

  5. 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.

  6. CS1010E Tutorial 2 Q5. • Additional pointer • No conflict in variable names from function to function.

  7. 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;

More Related