Download presentation
Presentation is loading. Please wait.
Published byAlban Mathews Modified over 8 years ago
1
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 3 (Functions) © CPCS 202 1432/1433 – Term 1
2
THE CONNECTION BETWEEN CHAPTERS In chapter 2, you learned to use functions, such as printf() and scanf(). In this chapter, you will learn to build new functions and use them in your programs. The new functions could be written within your file, or you can create a new file for them. Chapter [2] & Chapter [3] -2-
3
CHAPTER 3 - Functions 1. Introduction to Functions a) Function Header b) Call Function c) Function Library 2. Common Errors in Functions -3- # column shows the topics index. column shows the programs index. 1. Function (noArguments, noReturnValues) 2. The area and the circumference of the circle 3. Function (noArguments, 1 ReturnValue) 4. Function (1 Argument, 1 ReturnValue) 5. Function (1 Argument, noReturnValues) 6. Function (2 Arguments, noReturnValues) 7. Optimizing the Code
4
HUMAN & COMPUTER The good problem solver breaks any big problem into small pieces and solves each of them individually. For example, if you are always coming late to the class (problem), go over the reasons (sub-problems) that cause this, such as, waking up late, traffic, and homework. Now, solve each problem individually: Waking up late: Set the alarm one hour earlier. Traffic: leave your home half an hour earlier. Homework: Do it at the university. Solving a Big Problem without Panic -4-
5
HUMAN & COMPUTER Learn to use functions in your life to reduce thinking for a solution about similar problems. Let us say that you have Problem #1 in Day 1, which is: Problem 1: Sub-Problem 1,Sub-Problem 2,Sub-Problem 3 and, you solved all the sub-problems individually. In Day 2, you may have a similar Problem #2, which is: Problem 2: Sub-Problem 4,Sub-Problem 2,Sub-Problem 5 You will find that (1) you will solve the second problem faster after removing the redundancy in its elements, and (2) your solution is well organized. This is why you need to use functions in solving problems. Solving a Big Problem without Panic -5-
6
Functions A. Introduction Break the big problem into small problems. Each small problem can be solved individually. Write a function to solve each of these small problems. Functions can be shared or reused many times. Each function will have a separate memory, so the functions in one program do not share variables. B. Subtopics Function Header Call Function Function Library -6- 1
7
HUMAN & COMPUTER When you give a new job to someone, 1. Give him either the title of the job and the description, 2. Or give him only the title of the job and 1. Give him either the description to be read later. 2. or refer him to a location where the description is written. When you define a new function in a program, 1. either declare the function with the content, 2. or declare the function only and 1. Either put the content at the end of the program. 2. or include a library where the content is written. Defining a New Function -7-
8
Functions – Function Header A. Introduction Each function has to be declared before using it. There are two ways to declare functions: Declare the function only: similar to declaring variables some programmers like to declare a list of functions first, in order to organize the code. Then, they put the body / content of each function later. Declare the function with the content: similar to declaring and initial variables some programmers like to put the body of the function in the same time, in order to save space and time -8- 1a Function’s name needs to refer to its job Similar to declaring variables without initializing Similar to declaring variables with initializing
9
return_value function_name(arguments) { function_body } Functions – Function Header B 1. Syntax (declaring with content) C 1. Example void welcome_msg (void) { printf(“Hello World”); } -9- 1a int, double, char, void int, double, char, void The job of this function is to display a message on the screen. ---------------------------------------------- Function name: welcome_msg Arguments (inputs): void Return (output): void The job of this function is to display a message on the screen. ---------------------------------------------- Function name: welcome_msg Arguments (inputs): void Return (output): void VOID means nothing. You can replace it with an empty space.
10
return_value function_name(arguments); Functions – Function Header B 2. Syntax (declaring without content) C 2. Example void welcome_msg (void); Void bye_msg (void); : void welcome_msg (void) { printf(“Hello World”); } void bye_msg (void) { printf(“Good Bye”); } -10- 1a int, double, char, void int, char, double, void 1. Declaring a function indicates that the body of the function has to be written later 2. Declaring all the functions once makes the code trackable 1. Declaring a function indicates that the body of the function has to be written later 2. Declaring all the functions once makes the code trackable
11
HUMAN & COMPUTER They are 4 communication mechanisms between any employee in a job (function) and his boss (its caller): 1. Your boss gives you the job description and asks you to do it in your way. (no arguments and no return values) 2. Your boss gives you the job description and asks you for a report/output. (no arguments and a return value) 3. You boss gives you orders/inputs and asks you for your opinion/output. (with arguments and a return value) 4. Your boss gives you orders/inputs and asks you to execute them. (with arguments and no return values) Functions and their Callers -11-
12
Functions – Call Function A. Introduction In case you want to use a function in any place in the code, you need to call the function The function may return to a value (int, double, char) or not (void), so there are two ways to call a function B. Syntax C. Example welcome_msg(); Kilo = mile_to_kilo(70); -12- 1b function_name (arguments); return_value = function_name (arguments); without a return value with a return value
13
HUMAN & COMPUTER Some people prefer to keep the solutions for the small problems in a book, or they may have more than one book in their library. Storing the solutions in a library makes solving any new problem easier. Most programming languages have an associated core library. Core libraries typically include definitions for commonly used algorithms, data structures, and mechanisms for input and output. Good problem solvers like to build their own library. Core Libraries
14
Functions – Function Library A. Introduction If a function is defined in a different file, you need to #include the file (called header file) in your program. They are many files existing in the C Library to help you in writing a program faster and better. B. Syntax C. Example #include -14- 1c The file stdio.h has many functions, such as printf() and scanf() #include.h stands for “Header”
15
Functions C. Conclusion -15- 1 #include int msg1(int x); void msg2(void) { printf("Hi...\n"); } int main (void) { int retVal; msg2(); retVal = msg1(10); printf("%d\n", retVal); return(0); } int msg1(int x) { return(x+5); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. Declaring Function without content (1 argument & 1 return value) Call function (in the existing file) Content for the declared function Declaring Function with content (No argument & no return value) Call function (in stdio.h file) Library called stdio.h Call function (in the existing file)
16
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,noReturnValue) We need a program that can draw 2 types of houses under each other as shown in the following picture. The big house consists of a triangle and a square The small house consists of a triangle and a rectangle Look at the similarities between the two types and write one function for the similarity and 2 other functions for the others shapes. -16- P1
17
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,noReturnValue) Input Output Display the following shapes: Formula We have to use Functions, so look at the similarities -17- P1 /\ *------* | | *------* /\ *------* | | *------* triangle square triangle rectangle we need to build three functions to draw the two houses
18
Implementation Problem Analysis Design Outline Testing Maintenance 2.1 Display the following triangle: /\ *------* 1.2 Display the following square: *------* | | *------* void draw_triangle (void) Function (noArgument,noReturnValue) P1 int main (void) 1. Display a big house void draw_square (void) 1.1 Display the following triangle: /\ *------* 2.2 Display the following rectangle: *------* | | *------* 1.1 call draw_triangle() 1.2 call draw_square() void draw_square (void) 2.1 call draw_triangle() 2.2 call draw_square() 2. Display a small house 1 1 2 2 3 3 4 4 1. You can put the entire code in one function 3. Put a name for this new group (Function) 4. Call this function to execute its code Functions are able to reduce the redundancy in the code; call it again 2. Move the necessary statements into a new group
19
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,noReturnValue) P1 int main (void) 1. Display a big house 1.1 call draw_triangle() 1.2 call draw_square() 2. Display a small house 2.1 call draw_triangle() 2.2 call draw_rectangle() 1 1 void draw_triangle (void) 1. Display the following triangle: void draw_square (void) 1. Display the following square: void draw_rectangle (void) 1. Display the following rectangle: /\ *------* | | *------* | | *------* 2 2 3 3 4 4
20
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,noReturnValue) P1 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include void draw_triangle(void) { /* 1. Display a triangle shape */ } void draw_square(void) { /* 2. Display a square shape */ } void draw_rectangle(void) { /* 3. Display a rectangle shape */ } int main(void) { /* 1. Display a big house */ /* 2. Display a small house */ return (0); } Keep main() at the end to avoid declaring the other functions at the beginning
21
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,noReturnValue) P1 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. #include void draw_triangle(void) { /* 1. Display a triangle shape */ printf(" /\\ \n"); printf(“ / \\ \n"); printf(" *------* \n"); } void draw_square(void) { /* 1. Display a square shape */ printf(" *------* \n"); printf(" | | \n"); printf(" *------* \n"); } void draw_rectangle(void) { /* 1. Display a rectangle shape */ printf(" *------* \n"); printf(" | | \n"); printf(" *------* \n"); } int main(void) { /* 1. Display a big house */ draw_triangle(); draw_square(); /* 2. Display a small house */ draw_triangle(); draw_rectangle(); return (0); } To display the symbol ‘\’, you need to type it twice as shown
22
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,noReturnValue) P1 -22-
23
Implementation Problem Analysis Design Outline Testing Maintenance Area & Circumference of Circles We need a program to help users calculate the area and the circumference of a circle Users should enter the value of the radius Note: π = 3.14159 area = π × radius 2 circumference = 2 π × radius The symbol π is called Pi, and Pi has a constant value, which is 3.14159 -23- P2 Circumference Area
24
Implementation Problem Analysis Design Outline Testing Maintenance Area & Circumference of Circles Input radius Formula Area = 3.14159 × radius × radius Circumference = 2 × 3.14159 × radius Output Area Circumference -24- P2 radius Area Circum Area= 3.14159 x radius 2 Cirum=2 x 3.14159 x radius Any program has to have Inputs to process in order to get Outputs
25
Implementation Problem Analysis Design Outline Testing Maintenance Area & Circumference of Circles 1. Get the radius value radius 2. Calculate the area area = 3.14159 × radius × radius 3. Calculate the circumference circum = 2 × 3.14159 × radius 4. Display the area area 5. Display the circumference circum -25- P2 3.14159 is a constant value, so it can be defined within the constants
26
Implementation Problem Analysis Design Outline Testing Maintenance Area & Circumference of Circles P2 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. #include #define Pi 3.14159 int main(void) { double radius; // input – radius of a circle double area; // output – area of a circle double circum; // output – circumference /* 1. Get the circle radius */ /* 2. Calculate the area */ /* 3. Calculate the circumference */ /* 4. Display the area */ /* 5. Display the circumference */ return(0); } -26-
27
Implementation Problem Analysis Design Outline Testing Maintenance Area & Circumference of Circles P2 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include #define Pi 3.14159 int main(void) { double radius; // input – radius of a circle double area; // output – area of a circle double circum; // output – circumference /* 1. Get the circle radius */ printf("Enter radius> "); scanf("%lf", &radius); /* 2&3. Calculate the area & circumference */ area = Pi * radius * radius; circum = 2 * Pi * radius; /* 4&5. Display the area & circumference */ printf("The area is %.4f\n", area); printf("The circumference is %.4f\n", circum); return(0); }
28
Implementation Problem Analysis Design Outline Testing Maintenance Area & Circumference of Circles P2 28 Test Case 1 Test Case 2 Test Case 3
29
Implementation Problem Analysis Design Outline Testing Maintenance Area & Circumference of Circles The next three programs will use the idea of Program #2 and move parts of the codes into functions: Program #3 will move the INPUT into a new function (No Argument & One Return Value) Program #4 will move the FORMULA into a new function (One Argument & One Return Value) Program #5 will move the OUTPUT into a new function (One Argument & No Return Value) -29- P
30
Implementation Problem Analysis Design Outline Testing Maintenance O P P Area & Circumference of Circles P -30- I O P main() Program #2 I O P main() Program #3 int new(void) I O main() Program #4 int new(int) I main() Program #5 void new(int)
31
HUMAN & COMPUTER If we ask an author (programmer) to write a book (program) containing more than one chapter (function), and each chapter (function) will be read by a different person (different part of the memory). 1. Some prefer to start building the index (outline) including the chapters’ outline (functions’ outline), and then write the content (code) of each chapter (function) in any order. [P3] 2. Others prefer to consider the entire book (program) as one chapter (function), and then divide them into chapters (functions) after they complete writing the book (program). [as in Program #4] Ways to Build Functions
32
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,1 ReturnValue) We need a program that helps the users calculate the area and the circumference for a circle after entering the radius of the circle (Similar to Program 2) Build a function that gets the radius value from the user and returns the value to the caller; the headers of the functions is: double get_radius(void); This type of the function will do the job of reporting only to the caller -32- P3
33
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,1 ReturnValue) Input radius Formula Area = 3.14159 × radius × radius Circumference = 2 × 3.14159 × radius Output Area Circumference 3.14159 is a constant value, so it can be defined with the constants -33- P3
34
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,1 ReturnValue) The objective of each function has to be clear -34- P3 1. Get the radius value radius 1 1 get_radius () Return ( radius ) 1. Write the header of the function 3. It you need to return a value to the caller, use Return 4. Start writing the main function and consider the return values from other functions 2. Write the algorithm of the function main () 2 2 1. Get the radius value radius = get_radius () 2. Calculate the area area = 3.14159 × radius × radius 3. Calculate the circumference circum = 2 × 3.14159 × radius 4. Display the area area 5. Display the circumference circum No input 1 output The variable’s name in a external function can be different from the caller because each function has a different part of the memory.
35
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,1 ReturnValue) P3 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include #define Pi 3.14159 double get_radius(void) { double radius; /* 1. Get the radius value */ return (radius); } int main(void) { double radius; // input – radius of a circle double area; // output – area of a circle double circum; // output – circumference /* 1. Get the circle radius */ /* 2&3. Calculate the area & circumference */ /* 4&5. Display the area & circumference */ return(0); } -35-
36
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,1 ReturnValue) P3 -36- 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. #include #define Pi 3.14159 double get_radius(void) { double radius; /* 1. Get the radius value */ printf("Enter radius> "); scanf("%lf", &radius); return(radius); } int main(void) { double radius, area, circum; // 1 input, 2 outputs /* 1. Get the circle radius */ radius = get_radius(); /* 2&3. Calculate the area & circumference */ area = Pi * radius * radius; circum = 2 * Pi * radius; /* 4&5. Display the area & circumference */ printf("The area is %.4f\n", area); printf("The circumference is %.4f\n", circum); return(0); }
37
Implementation Problem Analysis Design Outline Testing Maintenance Function (noArgument,1 ReturnValue) P3 37 Test Case 1 Test Case 2 Test Case 3
38
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,1 ReturnValue) We need a program that helps the users calculate the area and the circumference for a circle after entering the radius of the circle (Similar to Program 2) Build 2 functions to calculate the area and the circumference; the headers of the functions are: double circle_area(double radius); double circle_circum(double radius); -38- P4
39
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,1 ReturnValue) Input radius Formula Area = 3.14159 × radius 2 Circumference = 2 × 3.14159 × radius Output Area Circumference Pi has a constant value, so it can be defined along with the constants -39- P4
40
Implementation Problem Analysis Design Outline Testing Maintenance circle_area ( radius ) 1. Calculate the area area = 3.14159 × radius 2 Return ( area ) circle_circum ( radius ) alculate the circumference circum = 2 × 3.14159 × radius Return ( circum ) main () 1. Get the radius value radius 2. Calculate the area area = 3.14159 × radius 2 3. Calculate the circumference circum = 2 × 3.14159 × radius 4. Display the area area 5. Display the circumference circum Function (1 Argument,1 ReturnValue) Move the processing steps from the main() to the new functions -40- P4 area = circle_area ( radius ) circum = circle_circum ( radius ) 1 1 3 3 2 2 We need to build a function that sends “radius” and returns “area” Just focus on the job of the function you are working on Select the variables names of the arguments and the return value ---------------------------------- You can select different variable’s names instead of “radius” and “area”
41
Implementation Problem Analysis Design Outline Testing Maintenance circle_area ( radius ) 1. Calculate the area area = 3.14159 × radius 2 Return ( area ) circle_circum ( radius ) 1. Calculate the circumference circum = 2 × 3.14159 × radius Return ( circum ) main () 1. Get the radius value radius 2. Calculate the area area = circle_area ( radius ) 3. Calculate the circumference circum = circle_circum ( radius ) 4. Display the area area 5. Display the circumference circum Function (1 Argument,1 ReturnValue) You can save the TIME of writing & the SPACE of the memory -41- 1 1 3 3 2 2 P4 1. Return (3.14159 × radius 2 ) 1. Return (2 × 3.14159 × radius )
42
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,1 ReturnValue) P4 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include #define Pi 3.14159 double circle_area(double radius) { double result; /* 1. Calculate the area */ return (result); } double circle_circum(double radius) { return (2 * Pi * radius); } int main(void) { double radius; // input – radius of a circle double area; // output – area of a circle double circum; // output – circumference /* 1. Get the circle radius */ /* 2&3. Calculate the area & circumference */ /* 4&5. Display the area & circumference */ return(0); } -42-
43
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,1 ReturnValue) P4 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. #include #define Pi 3.14159 double circle_area(double radius) { double result; /* 1. Calculate the area */ result = Pi * radius * radius; return (result); } double circle_circum(double radius) { return (2 * Pi * radius); } int main(void) { double radius; // input double area; // output – area of a circle double circum; // output – circumference /* 1. Get the circle radius */ printf("Enter radius> "); scanf("%lf", &radius); /* 2&3. Calculate the area & circumference */ area = circle_area (radius); circum = circle_circum (radius); /* 4&5. Display the area & circumference */ printf("The area is %.4f\n", area); printf("The circumference is %.4f\n", circum); return(0); } 1 st way (step-by-step) : 1.Declare a new variable 2.Store the result in the new variable 3.Return the value of the new variable 2 nd way (save space and time) : 1.return the result of the equation
44
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,1 ReturnValue) P4 44 Test Case 1 Test Case 2 Test Case 3
45
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,noReturnValue) We need a program that helps the users calculate the area and the circumference of a circle after entering the radius of the circle (Similar to Program 2) The output value of the area has to be inside a box as well as the circumference value. Build a function to draw a box around the value; the header of the function is: void display_box(double value); -45- P5
46
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,noReturnValue) Input radius Formula Area = 3.14159 × radius × radius Circumference = 2 × 3.14159 × radius Output Area Circumference Remember: Pi has a constant value, so it needs to be defined along with the constants -46- P5
47
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,noReturnValue) P5 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. #include #define Pi 3.14159 void display_box(double value) { /* 1. Display the value of the argument inside a box */ } int main(void) { double radius;// input – radius of a circle double area;// output – area of a circle double circum;// output – circumference /* 1. Get the circle radius */ /* 2. Calculate the area */ /* 3. Calculate the circumference */ /* 4. Display the area in a box */ /* 5. Display the circumference in a box */ return(0); } -47-
48
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,noReturnValue) P5 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. #include #define Pi 3.14159 void display_box(double value) { /* 1. Display the value of the argument inside a box */ printf("*----*\n"); printf("| %f |\n", value); printf("*----*\n"); } int main(void) { double radius; // input – radius of a circle double area; // output – area of a circle double circum; // output – circumference /* 1. Get the circle radius */ printf("Enter radius> "); scanf("%lf", &radius); /* 2&3. Calculate the area & circumference */ area = Pi * radius * radius; circum = 2 * Pi * radius; /* 4&5. Display the area & circumference in a box */ display_box(area); display_box (circum); return(0); }
49
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,noReturnValue) P5 You need to change the display format… WhY? Because it doesn’t look good
50
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,noReturnValue) P5 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. #include #define Pi 3.14159 void display_box(double value) { /* 1. Display the value of the argument inside a box */ printf("*---------*\n"); printf("| %7.4f |\n", value); printf("*---------*\n"); } int main(void) { double radius; // input – radius of a circle double area; // output – area of a circle double circum; // output – circumference /* 1. Get the circle radius */ printf("Enter radius> "); scanf("%lf", &radius); /* 2&3. Calculate the area & circumference */ area = Pi * radius * radius; circum = 2 * Pi * radius; /* 4. Display the area in a box */ printf("Area:\n"); display_box(area); /* 5. Display the circumference in a box */ printf("Circumference:\n"); display_box (circum); return(0); }
51
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,noReturnValue) P5 -51-
52
Implementation Problem Analysis Design Outline Testing Maintenance Function (1 Argument,noReturnValue) P5 -52- #include #define Pi 3.14159 void display_box(double value) { /* 1. Display the value of the argument... */ printf("*---------*\n"); printf("| %7.4f |\n", value); printf("*---------*\n"); } int main(void) { double radius; // input – radius of a circle double area; // output – area of a circle double circum; // output – circumference /* 1. Get the circle radius */ printf(“Enter radius> “); scanf(“%lf”, &radius); /* 2&3. Calculate the area & circumference */ area = Pi * radius * radius; circum = 2 * Pi * radius; /* 4&5. Display the area & circumference */ printf(“Area:\n”); display_box(area); printf(“Circumference:\n”); display_box (circum); return(0); } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. radius (double) area (double) circum (double) 5.078.5397531.4159 Memory: main() Output: value (double) 31.4159 Memory: display_box() – call 2 value (double) 78.53975 Memory: display_box() – call 1
53
Implementation Problem Analysis Design Outline Testing Maintenance Function (2 Arguments,noReturnValue) We need a program that takes 2 numbers from a user The two numbers will be sent to a function called simple_cal() that calculates and displays the Sum, Difference, Multiplication, and Division of the two numbers. -53- P6 simple_cal() int void int
54
Implementation Problem Analysis Design Outline Testing Maintenance Function (2 Arguments,noReturnValue) Input number 1 number 2 Formula No need because they are simple operators Output the sum of the two numbers the difference between the two numbers the multiplication of two numbers the division of the two numbers -54- P6
55
Implementation Problem Analysis Design Outline Testing Maintenance Function (2 Arguments,noReturnValue) main () 1. Get the first number from the user number1 2. Get the second number from the user number2 3. Calculate and display the results of the four simple operators 3.1 simple_math ( number1, number2 ) simple_math ( x, y ) 1. Calculate & display the sum of the two numbers x + y 2. Calculate & Display the different between the two numbers x – y 3. Calculate & Display multiplying the two numbers x * y 4. Calculate & Display dividing the two numbers x / y -55- P6 1 1 2 2
56
Implementation Problem Analysis Design Outline Testing Maintenance Function (2 Arguments,noReturnValue) P6 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. #include void simple_math(int x, int y) { /* 1. Calculate & Display the sum of the two numbers */ /* 2. Calculate & Display the different between the two numbers */ /* 3. Calculate & Display multiplying the two numbers */ /* 4. Calculate & Display dividing the two numbers */ } int main(void) { int number1, number2; // input: the two numbers /* 1. Get the first number from the user */ /* 2. Get the second number from the user */ /* 3. Calculate & Display the results */ return (0); }
57
Implementation Problem Analysis Design Outline Testing Maintenance Function (2 Arguments,noReturnValue) P6 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. #include void simple_math(int x, int y) { /* 1&2&3&4. Calculate & Display the +,-,*,/ of the two numbers */ printf("%d + %d = %d\n", x, y, x+y); printf("%d - %d = %d\n", x, y, x-y); printf("%d * %d = %d\n", x, y, x*y); printf("%d / %d = %f\n", x, y, x/y); } int main(void) { int number1, number2; // input: the two numbers /* 1&2. Get the two numbers from the user */ printf("Enter Number 1 > "); scanf("%d", &number1); printf("Enter Number 2 > "); scanf("%d", &number2); /* 3. Calculate & Display the results */ simple_math(number1, number2); return (0); }
58
Implementation Problem Analysis Design Outline Testing Maintenance Function (2 Arguments,noReturnValue) P6 The division operator doesn’t work; you need to unify the data-types -58-
59
Implementation Problem Analysis Design Outline Testing Maintenance Function (2 Arguments,noReturnValue) P6 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. #include void simple_math(int x, int y) { /* 1&2&3&4. Display the +,-,*,/ of the two numbers */ printf("%d + %d = %d\n", x, y, x+y); printf("%d - %d = %d\n", x, y, x-y); printf("%d * %d = %d\n", x, y, x*y); printf("%d / %d = %.2f\n", x, y, (double)x/(double)y); } int main(void) { int number1, number2; // input: the two numbers /* 1&2. Get the two numbers from the user */ printf("Enter Number 1 > "); scanf("%d", &number1); printf("Enter Number 2 > "); scanf("%d", &number2); /* 3. Calculate & Display the results */ simple_math(number1, number2); return (0); }
60
Implementation Problem Analysis Design Outline Testing Maintenance Function (2 Arguments,noReturnValue) P6 -60-
61
Implementation Problem Analysis Design Outline Testing Maintenance Optimizing the Code Write a program that finds the square root of two numbers and the square root of the sum of the two numbers. Hint: There is a library called math.h which has a function called sqrt() that receives a value and returns the square root of this value. The header of sqrt() is: double sqrt(double); If you are using an external function, you need to include it in the library -61- P7
62
Implementation Problem Analysis Design Outline Testing Maintenance Optimizing the Code Input number 1 number 2 Formula sum = number 1 + number 2 Output the square root of number 1 the square root of number 2 the square root of the sum -62- P7
63
Implementation Problem Analysis Design Outline Testing Maintenance P7 include main () 1. Get the first number from the user first 2. Get the second number from the user second 3. Calculate the square root of the first number first_sqrt = sqrt ( first ) 4. Calculate the square root of the second number second_sqrt = sqrt ( second ) 5. Calculate the sum of the two numbers sum = first + second 6. Calculate the square root of the sum sum_sqrt = sqrt ( sum ) 7. Display the square root of the 1st number first_sqrt 8. Display the square root of the 2nd number second_sqrt 9. Display the square root of the sum sum_sqrt # # Original Algorithm Optimizing the Code
64
Implementation Problem Analysis Design Outline Testing Maintenance P7 include main () 1. Get the first number from the user first 2. Calculate the square root of the first number first_sqrt = sqrt ( first ) 3. Display the square root of the first number first_sqrt 4. Get the second number from the user second 5. Calculate the square root of the second number second_sqrt = sqrt ( second ) 6. Display the square root of the second number second_sqrt 7. Calculate the sum of the two numbers sum = first + second 8. Calculate the square root of the sum sum_sqrt = sqrt ( sum ) 9. Display the square root of the sum sum_sqrt # # Alternative You may start with all the Inputs and finish with all the Outputs or take each group individually Optimizing the Code
65
Implementation Problem Analysis Design Outline Testing Maintenance P7 include main () 1. Get the first number from the user first 2. Calculate the square root of the first number first_sqrt = sqrt ( first ) 3. Display the square root of the first number first_sqrt 4. Get the second number from the user second 5. Calculate the square root of the second number second_sqrt = sqrt ( second ) 6. Display the square root of the second number second_sqrt 7. Calculate the sum of the two numbers and the its square root sum_sqrt = sqrt ( first + second ) 8. Display the square root of the sum sum_sqrt # # Optimization 1 you can include the formulas when you call functions to save time & space Optimizing the Code
66
Implementation Problem Analysis Design Outline Testing Maintenance P7 include main () 1. Get the first number from the user first 2. Get the second number from the user second 3. Calculate and display the square root of the 1 st number sqrt ( first ) 4. Calculate and display the square root of the 2 nd number sqrt ( second ) 5. Calculate and display the square root of the sum of the 2 numbers sqrt ( first + second ) # # Optimization 2 Sometimes you can include formulas in the outputs to reduce code size Count the number of lines and variables between the original algorithm and this one after optimization? Optimizing the Code
67
Implementation Problem Analysis Design Outline Testing Maintenance P7 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include // definitions of printf(), scanf() #include // definitions of sqrt() int main(void) { double first, second, sum, first_sqrt, second_sqrt, sum_sqrt; /* 1&2. Get the first & second numbers from the user */ printf("Enter the first number> "); scanf("%lf", &first); printf("Enter the second number> "); scanf("%lf", &second); /* 3&4&5&6. Calculate the square root of them */ first_sqrt = sqrt(first); second_sqrt = sqrt(second); sum = first + second; sum_sqrt = sqrt(sum); /* 7&8&9. Display the result */ printf("The square root of 1st number is %.2f\n", first_sqrt); printf("The square root of 2nd number is %.2f\n", second_sqrt); printf("The square root of the sum is %.2f\n", sum_sqrt); return (0); } Original Optimizing the Code
68
Implementation Problem Analysis Design Outline Testing Maintenance P7 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include // definitions of printf(), scanf() #include // definitions of sqrt() int main(void) { double first, second, first_sqrt, second_sqrt, sum_sqrt; /* 1&2. Get the first & second numbers from the user */ printf("Enter the first number> "); scanf("%lf", &first); printf("Enter the second number> "); scanf("%lf", &second); /* 3&4&5&6. Calculate the square root of them */ first_sqrt = sqrt(first); second_sqrt = sqrt(second); sum_sqrt = sqrt(first + second); /* 7&8&9. Display the result */ printf("The square root of 1st number is %.2f\n", first_sqrt); printf("The square root of 2nd number is %.2f\n", second_sqrt); printf("The square root of the sum is %.2f\n", sum_sqrt); return (0); } Optimization 1 Optimizing the Code
69
Implementation Problem Analysis Design Outline Testing Maintenance P7 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. #include // definitions of printf(), scanf() #include // definitions of sqrt() int main(void) { double first, second; // input /* 1&2. Get the first & second numbers from the user */ printf("Enter the first number> "); scanf("%lf", &first); printf("Enter the second number> "); scanf("%lf", &second); /* 3. Calculate and display the square root of the 1st number */ printf("The square root of 1st number is %.2f\n", sqrt(first)); /* 4. Calculate and display the square root of the 2nd number */ printf("The square root of 2nd number is %.2f\n", sqrt(second)); /* 5. Calculate and display the square root of the sum */ printf("The square root of the sum = %.2f\n", sqrt(first+second)); return (0); } Optimization 2 Optimizing the Code
70
Implementation Problem Analysis Design Outline Testing Maintenance P7 -70- Optimizing the Code
71
Common Errors in Functions If you want to place the main function before the other functions, you need to declare them before the main function. -71- 2 #include // functions declaration & body void math(int x) { printf("X = %d\n", x); } int main(void) { math(5); } #include // functions declaration void math(int x); int main(void) { math(5); } // functions body void math(int x) { printf("X = %d\n", x); } or 5
72
Evaluation Homework 1. Track the memory in Program 4 ? (follow the template in Program 5) 2. Write a function that converts dollars into riyals. Try three test cases on this function. Apply the Software Development Method to solve this problem. (Note: 1 Dollars = 3.76 Riyals) 3. Build a library called math_shapes.h that contains of the 2 functions described in Program 4: 1. circle_area() 2. circle_circum() and re-write the Program 4 by including the new library and removing the 2 functions from the code? (Show the two codes and the output in ONE page only) -72- DON’T forget to display your name and ID in the output hw
73
CHAPTER 3 - Functions 1. Introduction to Functions a) Function Header b) Call Function c) Function Library 2. Common Errors in Functions -73- # column shows the topics index. column shows the programs index. 1. Function (noArguments, noReturnValues) 2. The area and the circumference of circle 3. Function (noArguments, 1 ReturnValue) 4. Function (1 Argument, 1 ReturnValue) 5. Function (1 Argument, noReturnValues) 6. Function (2 Arguments, noReturnValues) 7. Performing 3 Square Root Computations
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.