Presentation is loading. Please wait.

Presentation is loading. Please wait.

FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating.

Similar presentations


Presentation on theme: "FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating."— Presentation transcript:

1 FUNCTIONS (CONT)

2 Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating point constants, by default, denote float type values. 24. Like variables, constants have a type. 25. Character constants are coded using double quotes. 26. Initialization is the process of assigning a value to a variable at the time of declaration. 27. The scanf function can be used to read only one value at a time. 28. All arithmetic operators have the same level of precedence. 29. The modulus operator % can be used only with integers. 30. The operators =, and != are all the same level of priority.

3 Midterm questions (31-40) 31. During modulo division, the sign of the result is positive, if both the operands are of the same sign. 32. In C, if a data item is zero, it is considered false. 33. The expression !(x y. 34. A unary expression consists of only one operand with no operators. 35. Associativity is used to decide which of several different expressions is evaluated first. 36. An expression statement is terminated with a period. 37. During the evaluation of mixed expressions, an implicit cast is generated automatically. 38. An explicit cast can be used to change the expression. 39. Parentheses can be used to change the order of evaluation expressions. 40. When if statements are nested, the last else gets associated with the nearest if without an else.

4 The keyword void is a data type in C ? It's considered a data type (for organizational purposes), but it is basically a keyword to use as a placeholder where you would put a data type, to represent "no data". Hence, you can declare a routine which does not return a value as: void MyRoutine(); But, you cannot declare a variable like this : void bad_variable; However, when used as a pointer, then it has a different meaning: void* vague_pointer; this declare a pointer, but without specifying what data type it is pointing to.

5 Floating point constants, by default, denote float type values. To confirm this statement the following program is implemented: So, by default all floating point variables are of type double

6 During modulo division, the sign of the result is positive, if both the operands are of the same sign.

7 Compiling Programs with Two or More Source Code Files The simplest approach to using several functions is to place them in the same file. When functions are too long it is better sort them in groups and save in separate files; Suppose that file1.c and file2.c are two files containing C functions. Linux –gcc file1.c file2.c –In addition, two object files called file1.o and file2.o are produced. –If you change file1.c you don’t need to recompile file2.c –gcc file1.c file2.o DOS Command-Line Compilers Similar to Linux, object files have.obj extension. a.out

8 Compiling Programs with Two or More Source Code Files Windows and Macintosh Compilers are project oriented. When you create a file you should include all your source code files (the ones with the.c extension) in the project. Microsoft Visual C++ Using header files If you put main() in one file and your function definitions in a second file, the first file still needs the function prototypes. Rather than type them in each time you use the function file, you can store the function prototypes in a header file. Make the #define directives available to each file. To do so, place the #define directives in a header file and then use the #include directive in each source code file.

9 The usehotel.c control module The hotel.h header file

10 The hotel.c Function Support Module The values of code are checked only after it is determined that scanf() succeeded in reading an integer value

11

12 Finding Addresses: The & Operator scanf() uses addresses & for arguments. The unary & operator gives you the address where a variable is stored. Any C function that modifies a value in the calling function without using a return value uses addresses! %p is the specifier for addresses 24 0B76will be printed out on the screen The address where pooh is stored is 0B76 You can think of the address as a location in memory!

13 loccheck.c -- checks to see where variables are stored the two poohs and bahs have different addresses just the value was transferred

14 Altering Variables in the Calling Function Suppose you have two variables called x and y and you want to swap their values: temp = x; x = y; y = temp; Nothing changed! Why?

15 Declaring Pointers A pointer is a variable (or, more generally, a data object) whose value is a memory address. int * pi; /* pi is a pointer to an integer variable */ char * pc; /* pc is a pointer to a character variable */ float * pf, * pg; /* pf, pg are pointers to float variables */ The declaration float* sunmass; says that feet is a pointer and that *sunmass is type float i.e. the value occupies 4 bytes starting from address float* sunmass; Memory can be represented as series of bytes Machine address that pointer contains 4 bytes

16 Pointers ptr = &pooh; /* assigns pooh's address to ptr */ ptr = &bah; /* make ptr point to bah instead of to pooh */ Now you can use the indirection operator (dereferencing operator) * to find the value stored in bah. val = *ptr; /* finding the value ptr points to */ The statements ptr = &bah; val = *ptr; taken together amount to the following statement: val = bah;

17 Using Pointers to Communicate Between Functions The program that uses pointers to make the interchange() function work. pointers Get memory address of variables

18 Review questions 1. Write C function headings for the following functions. (Just write the headings, not the functions’ bodies). donut() takes an int argument and prints that number of 0s. gear() takes two int arguments and returns type int. stuff_it() takes a double and the address of a double variable and stores the first value in the given location. n_to_char() takes and int argument and returns a char. digits() take a double argument and an int argument and returns an int. random() takes no argument and returns an int. 2. Devise a function called alter() that take two int variables, x and y, and changes their values to their sum and their difference, respectively.


Download ppt "FUNCTIONS (CONT). Midterm questions (21-30) 21. The underscore can be used anywhere in an identifier. 22. The keyword void is a data type in C. 23. Floating."

Similar presentations


Ads by Google