chapter 3-b.pdf
Transcript of chapter 3-b.pdf
User-defined functions
GENG 200 Marwan Alakhras
1
Functions has the following effects on your program
1. Prototype : necessary for calling (model for calling)
2. Definition: head and body, to perform the specific task
3. Calling : (invoking) using the function to carry on the task
The above classification is not the only valid one.
example1
GENG 200 Marwan Alakhras
2
Do you remember this
“Write a C program to solve the problem of converting
Fahrenheit temperature to Celsius”
Analysis
Input : Ftemp
Output Ctemp
Process
Ctemp=5/9(Ftemp-32)
Solution
GENG 200 Marwan Alakhras
3
Solution with New Structure(with functions)
GENG200 Marwan Alakhras
4
1. /* program structure */
2. #include <stdio.h>
3. double feh_to_cel(double); // Prototype ;
4. int main() /*function main begins program execution */
5. { double feh,cel;
6. scanf("%lf",&feh);
7. cel = feh_to_cel(feh); // calling ;
8. printf("\n result = %6.1f", cel);
9. return 0; /*indicate that program ended successfully*/
10.} /* end program function main */
Ma
in B
od
y
He
ad
11.double feh_to_cel(double a) /*function header */
12.{ double m; /*local variable */
13. m = (5.0/9.0)*(a-32);
14. return m; /* sends result back to cel in main line 7*/
15.} /* end function feh_to_cel*/
fun
ctio
n
de
finitio
n
prototypes
GENG 200 Marwan Alakhras
5
Prototype : model necessary for calling
It must be written before main program
It contains the following items 1. Return data type (int, double, char, or void for nothing)
2. Function name ( valid according to the rules), followed by ( )
3. Parameters list, inside ( )
1. How many
2. Type for each
3. Order
4. Names for every one (optional)
5. If there no parameter, then void
4. End with Semicolon ( ; )
Examples int fun(int a);
double xx(int a, double y, char);
void fun(char m, int x);
void f16(void);
double hi_m(void);
General Format:
Return_type f_name(parameter_list);
Function Definitions
Function definitions contains
Head (prototype, without semicolon ; )
Body (begins with { , and ends with } )
Return expression;
GENG 200
6
Marwan Alakhras
return-value-type function-name( parameter-list )
{
declarations and statements;
return expression;
}
Example double feh_to_cel(double a) /*function header */
{
double m; /*local variables */
m = (5.0/9.0)*(a-32); /* calculation statement */
return m; /* sends result back to the caller program */
} /* ends function */
Function Definitions
Definitions must occur after main body
Definitions and statements: function body (block)
Variables can be defined inside blocks (can be nested)
Variables declared inside the function can be used only inside function
Functions cannot be defined inside other functions
Returning control
If nothing returned
return;
or, until reaches right brace }
If something returned
return expression;
The expression in the return statement can be variable, constant, value, or function
But it must match the return-type
GENG 200
7
Marwan Alakhras
calling
GENG 200 Marwan Alakhras
8
Functions with are not used unless they are called In this course Function calling must occur within main function body or
other function body (no recursion) Calling occur according to the prototype or function header Functions are called with name and correct parameters list
Correct Name Correct Arguments lists and values
Name, order, type for each
Examples (according to the previous slide function)
double x = feh_to_cel(100.0);
temp2 = feh_to_cel(temp1);
Result = 1/temp1 + feh_to_cel(30 + temp1/80.0);
printf(“%6.2f”, feh_to_cel(temp1));
Result = pow(feh_to_cel(temp2), 3);
Calling (continue)
GENG 200 Marwan Alakhras
9
If the function has no return result (void)
Then calling must be alone on the line
Examples 1. void xx(int, double);
2. void yy(void);
Then the calling statements are respectively 1. xx(3,5.5);
2. yy();
Function Prototypes
Function prototype Function name
Parameters – what the function takes in
Return type – data type function returns (default int)
Used to validate functions
Prototype only needed if function definition comes after use in program
The function with the prototype
int maximum( int x, int y, int z );
Takes in 3 ints
Returns an int (single result)
Promotion rules and conversions Converting to lower types can lead to errors
GENG 200
10
Marwan Alakhras
Example 2
GENG 200 Marwan Alakhras
11
Based on example 3.3 page 142,
Modify the program to have function side3, which receives values for b, c, and angle α in RAD, then returns the third side length value as:
a2=b2+c2 - 2bc.cos(α)
Solution
GENG 200 Marwan Alakhras
12
1. #include<stdio.h>
2. #include<math.h>
3. #define bi 22.0/7.0
4. double side3(double x, double y, double angle);
5. int main(){
6. double a,b,c,alpha;
7. printf("\n Enter values for two sides and angle"
8. "\nbetween them in Deg : ");
9. scanf("%lf%lf%lf",&b,&c,&alpha);
10. a=side3(b,c,alpha*bi/180);
11. printf("\nThe third side is %.2f \n",a);
12. return 0;
13. }
14. double side3(double x, double y, double angle){
15. return sqrt(pow(x,2)+pow(y,2)-2*x*y*cos(angle));
16. }
Output screen
GENG 200 Marwan Alakhras
13
Enter values for two sides and angle
between them in Deg : 3 4 90
The third side is 5.00
Example 3
GENG 200 Marwan Alakhras
14
Modify the previous example2 by adding another function GET_data(); to get the data from the user one by one;
Hint: this function should have no arguments, but returned value
solution
GENG 200 Marwan Alakhras
15
1.#include<stdio.h>
2.#include<math.h>
3.#define bi 22.0/7.0
4.double side3(double x, double y, double angle);
5.double GET_data(void);
6.int main(){
7. double a,b,c,alpha;
8. printf("\nEnter values for two sides and angle "
9. "\nbetween them in Deg : ");
10. b=GET_data();
11. c=GET_data();
12. alpha=GET_data();
13. a=side3(b,c,alpha*bi/180);
14. printf("\n The third side is %.2f \n",a);
15. return 0;
16.}
Solution (continue)
GENG 200 Marwan Alakhras
16
17. double side3(double x, double y, double angle){
18. return sqrt(pow(x,2)+pow(y,2)-2*x*y*cos(angle));
19. }
20. double GET_data(void){
21. double x;
22. scanf("%lf",&x);
23. return x;
24. }
Enter values for two sides and angle
between them in Deg : 3 4 90
The third side is 5.00
Example 4
GENG 200 Marwan Alakhras
17
Modify the last program (example3) by adding another function DISPLAY to show the final result on the screen
solution
GENG 200 Marwan Alakhras
18
1.#include<stdio.h>
2.#include<math.h>
3.#define bi 22.0/7.0
4.double side3(double x, double y, double angle);
5.double GET_data(void);
6.void DISPLAY(double);
7.int main(){
8. double a,b,c,alpha;
9. printf("\nEnter values for two sides and angle "
10. "\nbetween them in Deg : ");
11. b=GET_data();
12. c=GET_data();
13. alpha=GET_data();
14. a=side3(b,c,alpha*bi/180);
15. DISPLAY(a);
16. return 0;
17.}
Solution (continue)
GENG 200 Marwan Alakhras
19
18. double side3(double x, double y, double angle){
19. return sqrt(pow(x,2)+pow(y,2)-2*x*y*cos(angle));
20. }
21. void DISPLAY(double x){
22. printf("\n The third side is %.2f \n",x);
23. }
24. double GET_data(void){
25. double x;
26. scanf("%lf",&x);
27. return x;
28. }
Enter values for two sides and angle
between them in Deg : 3 4 90
The third side is 5.00