Unformatted text preview:

12/7/2012Instructor: Joel Castellanose-mail: [email protected]: http://cs.unm.edu/~joel/Office: Farris Engineering Center (FEC) room 319Functions and Scope with ArraysCS 241 Data Organization using CRead: Kernighan & Ritchie Due Tuesday, Feb 72.3: Constants2.4: Declarations2.5: Arithmetic Operators2.6: Relational and Logical Operators2.7: Type Conversions2.8: Increment and Decrement Operators Due Thursday, Feb 92.9: Bitwise Operations Supplemental reading on class website:Math is Fun: binary Number SystemLab 3: Variation of setbits (Exercise 2-6)22Function Prototype and Definition1) #include <stdio.h>2)3)int foo(int x);4)5)void main(void)6) { int n=5;7) printf("%d\n", foo(n));8) }9)10)int foo(int n)11){ return 2*n;12)}3Function PrototypeFunction DefinitionOutput:10Must AgreeFunction Prototype and Definition1) #include <stdio.h>2)3)int foo(int x);4)5)void main(void)6) { int n=5;7) printf("%d\n",foo(n));8) }9)10)int foo(int n)11) { return 2*n;12) }4A Prototype is needed when:1) A function is used in a line above where it is defined.2) A function is defined in a different file.1) //Prototype of foo not needed.2) #include <stdio.h>3)4)int foo(int n)5) { return 2*n;6) }7)8)void main(void)9) { int n=5;10) printf("%d\n",foo(n));11) }3Quiz: Section 1.7: Functions1) #include <stdio.h>2)3)int foo(float x);4)5)void main(void)6) { int n=5;7) printf("%d\n", foo(n));8) }9)10)int foo(int n)11) { return 2*n;12) }5This code will not compile because:a) The version of foo in line 3 accepts a float, but returns an int.b) The function foo in line 3 has no body.c) The version of foo in line 3 should not end with a semicolon.d) The variable n is declared in two different places.e) The prototype of foo does not agree with the definition.No Overloaded Functions in C1) #include <stdio.h>2)3)int foo(int n)4) { return 2*n;5) }6)7)int foo(int k, int n)8) { return k*n;9) }10)11)void main(void)12) { int n=5;13) printf("%d\n", foo(n));14) printf("%d\n", foo(3,n);15) }6foo.c:7: error: conflicting types for ‘foo’4Quiz: Section 1.7: Functions1) #include <stdio.h>2) int foo(int n)3) { n = 2*n;4) printf("foo: n=%d ", n);5) return n;6) }7)8)void main(void)9) { int n=5;10) printf("main: foo(n)=%d, n=%d\n", foo(n),n);11) }7The output of this C program is:a) foo: n=5 main: foo(n)=5, n=5b) foo: n=10 main: foo(n)=10, n=5c) foo: n=10 main: foo(n)=10, n=10d) foo: n=10 main: foo(n)=20, n=10Scope of a Variable in CAll constants and variables have scope: The values they hold are accessible in some parts of the program, where as in other parts, they don't appear to exist.Block Scope: variables declared in a block are visible between an opening curly bracket and the corresponding closing bracket.Function Scope: variables visible within a whole function.File Scope: variables declared static and outside all function blocks. Program Scope (global variables): variables declared outside all function blocks. 85Program Scope and Function Scope91. #include <stdio.h>2. int a=4;3. int b=7;4. void foo()5. { 6. int b = 12;7. a++;8. printf("foo: a=%d, b=%d\n", a, b);9. } 10.11.void main(void)12.{ 13. foo();14. printf("main: a=%d, b=%d\n", a, b);15.}Output: ????foo does not return a value but it has two side effects:1) Sends data to the standard output stream.2) Changes a global field: a.foo: a=5, b=12main: a=5, b=7Quiz (sect 1.8): Call by ValueIn the C Programming Language, call by value means:a) When two functions have the same name, the compiler determines which to call by the value of the arguments. b) The called function is given the address of its arguments so that the function can both read and set the argument’s values.c) Each called function is assigned a value that is used by the operating system to determine the function’s priority. This is most useful on multi-core systems.d) The called function is given the values of its arguments which are copied into temporary variables.106Quiz (sect 1.10): Automatic VariablesIn the C Programming Language, an automatic variable is:a) A local variable in a function which comes into existence at the time the function is called, and disappears when the function is exited. b) A variable that is automatically initialized.c) A global variable that is automatically available to all functions within the source file.d) A global variable that is available to all functions within any source file that declare the variable as extern.e) A variable that is automatically defined by the compiler such as PI, E, and HBAR.11Increment Elements of Global Array: 1 of 2121. #include <stdio.h>2.3.#define DATA_COUNT 44. #define MAX_VALUE 32 5.6.int x[DATA_COUNT];7.8.int increment(void)9. { // Adds 1 to each element of global array x[]. // Returns error if any element of x[] is > MAX_VALUE15. }16.void main(void)17. { //Sets initial values of x[]. //Calls increment() some number of times.25. }Global arrayNote: this violates our standard: x is too short a name for a global variable.7Increment Elements of Global Array: 2 of 2138. int increment()9. { int i;10. for (i=0; i<DATA_COUNT; i++)11. { if (x[i] >= MAX_VALUE) return 1;12. x[i]++;13. }14. return 0;15. }16. void main(void)17. { x[0] = 20; x[1] = 15; x[2] = 30; x[3] = 2;18. int i;19. for (i=0; i<5; i++)20. { if (increment()) printf("ERROR\n");21. else22. { printf("%d %d %d %d\n", x[0],x[1],x[2],x[3]);23. }24. }25. }Output:21 16 31 322 17 32 4ERRORERRORERRORIncrement Elements of Global Array: 2 of 2148. int increment()9. { int i;10. for (i=0; i<DATA_COUNT; i++)11. { if (x[i] >= MAX_VALUE) return 1;12. }13.14.for (i=0; i<DATA_COUNT; i++)15. { x[i]++;16. }17. return 0;18.}Output:21 16 31 322 17 32 4ERRORERRORERROR8What Does the fibonacci Function Do?151. #include <stdio.h>2.3.void fibonacci(int n0, int n1)4. { int n2 = n0 + n1;5. n0 = n1;6. n1 = n2;7. }8.9.void main(void)10. { int n0 = 1;11. int n1 = 1;12. int i;13. for (i=1; i<10; i++)14. { printf("%d ", n0);15. fibonacci(n0, n1);16. }17. printf("\n");18. }Output:1 1 1 1 1 1 1 1 1Nothing!!!fibonacci does not return a value.fibonacci has no side effects.■ The body of fibonacci is unchanged from the last program.■ In the last version, n0 and n1 were local to fibonacci.■ In this version, n0 and n1 are global.■ Therefore, this version of fibonacci has side effects.Fibonacci on Global Variables161. #include <stdio.h>2. int n0, n1;3.4.void fibonacci()5. { int n2 = n0 + n1;6. n0 = n1;7. n1 = n2;8. }9.10.void main(void)11. { n0 = 1; n1 = 1;12. int i;13. for (i=1; i<10; i++)14. { printf("%d


View Full Document

UNM CS 241L - CS 241L LECTURE NOTES

Download CS 241L LECTURE NOTES
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view CS 241L LECTURE NOTES and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view CS 241L LECTURE NOTES 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?