C Programming Introduction Part II Geb Thomas Learning

C Programming Introduction Part II Geb Thomas

Learning Objectives Learn how to use C functions Learn how to use C arrays Understand what a pointer is and how to use it Understand how to open a file and write to it.

C Functions Generally, you pass variables to a function, it does some work, and returns a variable. double Sqrt(double val) – a function that takes a value and returns its square root. double pow(double v 1, double v 2) – a function that raises v 1 to the power v 2. Sometimes you don’t need or want a return value (such as the function to close a file). Sometimes you don’t need to pass anything to the function or get anything back (such as a function to clear the monitor of any data).

Declaring C Functions All functions should be declared at the top of the program (before they are used). int count(void); int open. File(char *file. Name); The declaration tells the compiler how the function should be used -- what it should take and what it should return.

Defining Functions The function is defined in the body of the c-file, outside of any other functions. The definition explains what the function does with the information passed in and how it determines the information to return. int factorial(int in. Val) { /* start of function definition */ int cntr, return. Val = 1; /*declaration of local variables */ for (cntr = 2; cntr <= in. Val; cntr++) /* note use of “in. Val” */ return. Val = return. Val*cntr; return. Val; /* function will return this value */ } /* end of function definition */

Using Functions To use a function, just pass variables or arguments in the correct order. You can use the return value to set another variable. int main(int argc, void **argv) { int a = 4, b; b = factorial(a); b = factorial(6); }
![C Arrays C uses square brackets for defining and using arrays. double F[6]; declares C Arrays C uses square brackets for defining and using arrays. double F[6]; declares](http://slidetodoc.com/presentation_image_h2/d596014c78697d1fb1b15ac436733f27/image-7.jpg)
C Arrays C uses square brackets for defining and using arrays. double F[6]; declares a six-dimensional vector. The six values in f are ordered starting from 0. F[0] = 3; /*set 1 st element to 3. */ F[3] = 14. 1234; /* set 4 th element to 14 … */ F[6] = 234. 341; /* error, this would be the 7 th element in a six-dimensional vector */ You can pass element values to functions F[4] = sqrt (F[3]);

Pointers C allows the programmer to access specific locations in memory with pointers. Pointers user two operators: & the “address of” operator * the “indirection” or “dereferencing” operator

How Pointers Work Instead of referring to a variable directly, pointers provide the location where the variable exists in memory. This is useful when you want to change a variable in memory within a function. Normally a function copies its arguments and changes to the passed arguments to not affect the main program. If you pass a pointer, then when you change the values at the address pointed to, the changes DO affect the main program.

How to Use Them Declare a pointer to a float: float *float. Ptr; Declare a pointer to a double; double *dbl. Ptr; Set the pointer to a variable. float. Ptr = &my. Float. Variable; Set the value in the pointer *float. Ptr = 6; Use the value in a pointer printf(“Pointer value is %fn”, *float. Ptr);

File Access FILE *fptr; /* pointer to a file structure */ fptr = fopen(“filename”, “r+”); fprintf(“Print this %f, babyn”, val); fscanf(“%f”, &val); /* reads a value */ fclose(fptr);

Learning Objectives Learn how to use C functions Learn how to use C arrays Understand what a pointer is and how to use it Understand how to open a file and write to it.
- Slides: 12