320 likes | 419 Views
ecs30 Winter 2012: Programming and Problem Solving # 05: Chapters 2 ~ 5. Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu. kms = 1.609 * miles ; . “ = ” : assignment from right to left
E N D
ecs30 Winter 2012:Programming and Problem Solving#05: Chapters 2~5 Dr. S. Felix Wu Computer Science Department University of California, Davis http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu ecs30 WInter 2012 Lecture #05
kms = 1.609 * miles; • “ = ” : assignment from right to left • the LEFT side is always a declared variable! • The RIGHT side can be: • variables (including [&, *]), constants, logical/arithmetic expressions kms = 1.609 * miles; ecs30 WInter 2012 Lecture #05
Assignment ecs30 WInter 2012 Lecture #05
Arithmetic expressions V = p2-p1/t2-t1; • +, -, *, /, % ecs30 WInter 2012 Lecture #05
Evaluation Expressions(section 2.5 and 4.2) • Parenthesis rule • Operator precedence rule • function calls, unary {+,-,&,*,!}, binary {*,/,%}, binary {+,-}, logic {<,<=,>=,>},… assignment {=} • Associativity rule • Unary: right to left (e.g.,(-*x)) • Binary: left to right (e.g., (x / y * z)) ecs30 WInter 2012 Lecture #05
Readability • x/y*z/w+a/b--c*-*d • First of all, you shouldn’t write such a long expression! • (((x/y)*z)/w)+(a/b)-((-c)*(-(*d))) ecs30 WInter 2012 Lecture #05
Reusability/Sharing, Readability/Debugging, Modularity Abstraction Function vs. Macro ecs30 WInter 2012 Lecture #05
Function • intmain (void) { … } • float myfunc (intx) { …; return f;} <return type> <function name> (input parameters) {<body>} • { <declarations> <statements> } ecs30 WInter 2012 Lecture #05
#define PI 3.14159 double find_area (double r) { double area; area = r * r * PI; return area; } ecs30 WInter 2012 Lecture #05
Inputs and Outputs (parameters and return value) ecs30 WInter 2012 Lecture #05
Homework Assignment #2 % gcc -E ecs30b_hw2_2.c % gcc -c ecs30b_hw2_2.c % gcc ecs30b_hw2_2.o -o ecs30b_hw2_2 -lm Chapter 3: mathematical library functions ecs30 WInter 2012 Lecture #05
Call by Value, Reference, Name Pointer/Address of Memory/Variable Pointer to Function ~ Control of “where is the function code”! ecs30 WInter 2012 Lecture #05
Computing under the same formula… The only difference is input parameter: d1 or d2 ecs30 WInter 2012 Lecture #05
double Find_area(doubler) { double area; area = r * r * PI; return area; } ecs30 WInter 2012 Lecture #05
Call_by_Value ---- (Call_by_Reference) double r_out, r_in, the_area; the_area = r_out * r_out * PI – r_in * r_in * PI; the_area = find_area(r_out) – find_area(r_in); ecs30 WInter 2012 Lecture #05
Section 13.8 #define AREA(r) (r * r * PI) double r_out, r_in, the_area; the_area = r_out * r_out * PI – r_in * r_in * PI; the_area = find_area(r_out) – find_area(r_in); the_area = AREA(r_out) – AREA(r_in); ecs30 WInter 2012 Lecture #05
double r_in, r_out, the_area; get_input_paramenters(); calculating_area(r_in, r_out); print_output(the_area); void get_input_paramenters(void) { double r_in, r_out; scanf(“%lf”, &r_in); scanf(“%lf”, &r_out); return 0; } ecs30 WInter 2012 Lecture #05
Scope { … } • { … } • {… { … {…}…} … {…}…} • { <declarations> <statements> } ecs30 WInter 2012 Lecture #05
double r_in, r_out, the_area; get_input_paramenters(r_in, r_out); calculating_area(r_in, r_out); print_output(the_area); void get_input_paramenters(double in, out) { scanf(“%lf”, &in); scanf(“%lf”, &out); return 0; } ecs30 WInter 2012 Lecture #05
double r_in, r_out, the_area; get_input_paramenters(&r_in, &r_out); calculating_area(r_in, r_out); print_output(the_area); void get_input_paramenters(double *in_ptr, *out_ptr) { scanf(“%lf”,in_ptr); scanf(“%lf”,out_ptr); return 0; } ecs30 WInter 2012 Lecture #05
Reusability/Sharing, Readability/Debugging, Modularity Abstraction Single return value for now; We will address other ways later (in Chapter 6). Function vs. Macro ecs30 WInter 2012 Lecture #05
Inputs and Outputs (parameters and return value) They are actually different!! ecs30 WInter 2012 Lecture #05
An interesting difference • Input/output for functions • A design concept in problem solving • What should be input to a function/module? • Call by Value/Referene/Name • Parameters and Return values for functions • An implementation concept in C (or a programming language feature of C) • We can use this feature to implement different ways of inputs and outputs! • C is call by value!(but…) Advanced ecs30 WInter 2012 Lecture #05
Execution Sequence/Control Flow 3 1 2 ecs30 WInter 2012 Lecture #05
X n ecs30 WInter 2012 Lecture #05
X n >>> x = x * 2; ecs30 WInter 2012 Lecture #05
How about num_1 and num_2? num_1 X num_2 n Caller Callee When you make a function call, do they “SHARE” the memory boxes? In other words, 12 or 24 bytes allocated for these four variables? ecs30 WInter 2012 Lecture #05
No, they don’t share. Calling means “copying the values!” ecs30 WInter 2012 Lecture #05
5.0 x = x * 2; Caller No, they don’t share. Calling means “copying the values!” Callee ecs30 WInter 2012 Lecture #05
num_1 X num_2 n ecs30 WInter 2012 Lecture #05
num_1 X num_2 n Functions in C are“call by values”for input parameters! I.e., we copy the values fromcallertocalleewhencalling a function! (and, NOT copying them back when returns!) ecs30 WInter 2012 Lecture #05