1 / 35

Fun fun ’til daddy takes your C turbo away!

Fun fun ’til daddy takes your C turbo away! A C program is organized into one or more modules (source files created by a programmer), within which are various functions and variables . As a minimum the ‘ main ’ function must exist. A.Honey (Nov2003). S3-1. Fun fun fun functions

jens
Download Presentation

Fun fun ’til daddy takes your C turbo away!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fun fun ’til daddy takes your C turbo away! A C program is organized into one or more modules (source files created by a programmer), within which are various functions and variables. As a minimum the ‘main’ function must exist. A.Honey (Nov2003) S3-1

  2. Fun fun fun functions A C program is organized into one or more modules (source files created by a programmer), within which are various functions and variables. As a minimum the ‘main’ function must exist. Functions are the C equivalent of subroutines. Functions allow a program to be broken into smaller more manageable and maintainable pieces. A.Honey (Nov2003) S3-2

  3. Fun fun fun functions A C program is organized into one or more modules (source files created by a programmer), within which are various functions and variables. As a minimum the ‘main’ function must exist. Functions are the C equivalent of subroutines. Functions allow a program to be broken into smaller more manageable and maintainable pieces. Each function has a set of input and output requirements (declarations and arguments) that must be adhered to by any program which uses that function. A.Honey (Nov2003) S3-3

  4. Function return type Each function also has a type, which is referred to as the functions ‘returntype’. The return type is immediately in front of the function name. If you neglect to give a function a return type then it defaults to ‘int’. A.Honey (Nov2003) S3-4

  5. Function return type Each function also has a type, which is referred to as the functions ‘returntype’. The return type is immediately in front of the function name. If you neglect to give a function a return type then it defaults to ‘int’. At least one returnstatement must exist within the function and, that return statement must specify the ‘value’ to be returned. I use ‘value’ lightly because the function’s type and, therefore, the returned value, need not be a simple C type (integer, float, double, char). The return type can be a very complex ‘type’ created by the programmer. Alternatively, you can specify that a function’s type is ‘void’. In this case a return statement is not necessary but if it exists it must not have a return value. A.Honey (Nov2003) S3-5

  6. Function arguments Every function may have a list of ‘calling arguments’. A function’s arguments appear as a comma separated list of variable declarations, enclosed in parenthesis, immediately following the function’s name. A.Honey (Nov2003) S3-6

  7. Function arguments Every function may have a list of ‘calling arguments’. A function’s arguments appear as a comma separated list of variable declarations, enclosed in parenthesis, immediately following the function’s name. There is an upper limit to the number of arguments that a function may have. This limit is somewhat compiler dependent but typically 10 is the maximum. A.Honey (Nov2003) S3-7

  8. Function arguments Every function may have a list of ‘calling arguments’. A function’s arguments appear as a comma separated list of variable declarations, enclosed in parenthesis, immediately following the function’s name. There is an upper limit to the number of arguments that a function may have. This limit is somewhat compiler dependent but typically 10 is the maximum. However, it is perfectly valid to have a function with no calling arguments. A.Honey (Nov2003) S3-8

  9. Creating a function Let’s create a simple function of type float, which has four floating-point arguments, say c1, c2, c3, and x. The function should return the result of evaluating the polynomial: c1*x*x + c2*x + c3. A.Honey (Nov2003) S3-9

  10. Creating a functionS3_pgm1 Let’s create a simple function of type float, which has four floating-point arguments, say c1, c2, c3, and x. The function should return the result of evaluating the polynomial: c1*x*x + c2*x + c3. The function declaration should be similar to: float polyfun1(float c1,float c2,float c3,float x) A.Honey (Nov2003) S3-10

  11. Creating a functionS3_pgm1 Let’s create a simple function of type float, which has four floating-point arguments, say c1, c2, c3, and x. The function should return the result of evaluating the polynomial: c1*x*x + c2*x + c3. The function declaration should be similar to: float polyfun1(float c1,float c2,float c3,float x) { return(c1*x*x+c2*x+c3); } Create the main routine and use command line arguments to test your function. A.Honey (Nov2003) S3-11

  12. More functionsxtank Let’s build an xtank program. What is an xtank you ask? A.Honey (Nov2003) S3-12

  13. More functionsxtank Let’s build an xtank program. What is an xtank you ask? An xtankprogram is used to control an ‘x’ on the screen. By holding down certain keys the operator can drive the tank (x) around the screen. A.Honey (Nov2003) S3-13

  14. More functionsxtank Let’s build an xtank program. What is an xtank you ask? An xtankprogram is used to control an ‘x’ on the screen. By holding down certain keys the operator can drive the tank (x) around the screen. Assume ‘int joystick()’ exists and returns: 1 if the joystick is pushed forward; 2 for reverse; 3 for left; 4 for right; 0 for stopped, and the previous value is returned if no key is pressed. We will respectively equate these to the ‘f’, ’b’, ’l’, ‘r’, and ‘s’ keys. A.Honey (Nov2003) S3-14

  15. More functionsxtank These are the other functions you need: void motor_on() void forward() void reverse() void left() void right() void alloff() In main you must call motor_on() once and then create an infinite loop to repeatedly call joystick(), for input, and, based on the return value, call the appropriate motor control function. Don’t forget to pause for a second each iteration or everything will be too fast for your eyes! A.Honey (Nov2003) S3-15

  16. More functionsxtank Notice that we grouped the functions that are similar into modules. For instance motor.c contains all the functions to move the xtank. I am not too concerned if you understand what each of those functions does as their statements are simply relevant to moving around the screen on Win. A.Honey (Nov2003) S3-16

  17. More functionsxtank Notice that we grouped the functions that are similar into modules. For instance motor.c contains all the functions to move the xtank. I am not too concerned if you understand what each of those functions does as their statements are simply relevant to moving around the screen on Win. Also notice that we grouped constants, that need to be used in more than one module, into an ‘include file’, namely, tank.h. The modules which need those constants then simply have a preprocessor #include statement. A.Honey (Nov2003) S3-17

  18. More functionsxtank Notice that we grouped the functions that are similar into modules. For instance motor.c contains all the functions to move the xtank. I am not too concerned if you understand what each of those functions does as their statements are simply relevant to moving around the screen on Win. Also notice that we grouped constants, that need to be used in more than one module, into an ‘include file’, namely, tank.h. The modules which need those constants then simply have a preprocessor #include statement. The include file also contains the ‘function prototypes’ for each of the functions in the various modules. The prototypes allow the compiler to validate the calling parameters, thereby, protecting the programmers from themselves! A.Honey (Nov2003) S3-18

  19. More functionsxtank Now create a function ‘void move( int direction)’ that you call, instead of performing the nested if statements within main. You will essentially put the nested if within move(). A.Honey (Nov2003) S3-19

  20. More functionsxtank Now create a function ‘void move( int direction)’ that you call, instead of performing the nested if statements within main. You will essentially put the nested if within move(). The while loop, within main(), can then be simplified to: While (1) { move( joystick() ); sleep( 1000.0 ); } A.Honey (Nov2003) S3-20

  21. Point me too the right location! A ‘pointer’ is used to hold theaddress of a memory location containing some data. A.Honey (Nov2003) S3-21

  22. Pointers! A ‘pointer’ is used to hold the address of a memory location containing some data. The declaration of a pointer is similar to what we have seen for the argument declarations of main(), namely: <type> *<variable name>; Such as: int *ivar; float *fvar; A.Honey (Nov2003) S3-22

  23. Pointers! A ‘pointer’ is used to hold the address of a memory location containing some data. The declaration of a pointer is similar to what we have seen for the argument declarations of main(), namely: <type> *<variable name>; Such as: int *ivar; float *fvar; When the compiler encounters a variable, that has been declared as a pointer, the compiler knows that the data is a pointer, or memory address, and can attempt to ensure that it is used correctly. A.Honey (Nov2003) S3-23

  24. Pointers! Assignmentto a pointer depends somewhat on the context but in general you must provide the address of another data value. A.Honey (Nov2003) S3-24

  25. Pointers! Assignment to a pointer depends somewhat on the context but in general you must provide the address of another data value. The syntax to do that is identical to the way we passed parameters to the scanf() function, namely, by preceding the variable name by an ampersand. An example of scanf: int ivar; float fvar; scanf( “%d %f”,&ivar, &fvar ); A.Honey (Nov2003) S3-25

  26. Pointers! Assignment to a pointer depends somewhat on the context but in general you must provide the address of another data value. The syntax to do that is identical to the way we passed parameters to the scanf() function, namely, by preceding the variable name by an ampersand. An example of scanf: int ivar; float fvar; scanf( “%d %f”,&ivar, &fvar ); So pointer use would be something like: int *myptr; myptr = &ivar; Thereby, assigning the address of ivar to myptr. A.Honey (Nov2003) S3-26

  27. Pointers! When the actual value of a pointer is needed that pointer must be ‘de-referenced’. A.Honey (Nov2003) S3-27

  28. Pointers! When the actual value of a pointer is needed that pointer must be ‘de-referenced’. The syntax for de-referencing is to simply insert the ‘*’ in front of the variable: int xval = 1234567890; int*myptr; myptr = &xval; *myptr = 9876; printf(“myptr=%d, xval=%d\n”,*myptr, xval); A.Honey (Nov2003) S3-28

  29. Pointers! When the actual value of a pointer is needed that pointer must be ‘de-referenced’. The syntax for de-referencing is to simply insert the ‘*’ in front of the variable: int xval = 1234567890; int*myptr; myptr = &xval; *myptr = 9876; printf(“myptr=%d, xval=%d\n”,*myptr, xval); The statement before the printf instructs the compiler to put 9876 into the memory location pointed to by myptr. So tell me, what is going to be printed? A.Honey (Nov2003) S3-29

  30. Pointers! When the actual value of a pointer is needed that pointer must be ‘de-referenced’. The syntax for de-referencing is to simply insert the ‘*’ in front of the variable: int xval = 1234567890; int*myptr; myptr = &xval; *myptr = 9876; printf(“myptr=%d, xval=%d\n”,*myptr, xval); The statement before the printf instructs the compiler to put 9876 into the memory location pointed to by myptr. So tell me, what is going to be printed? Yep 9876 twice! A.Honey (Nov2003) S3-30

  31. Pointers! One of the primary reasons for pointers is to be able to ‘pass by reference’ parameters into functions, so that those functions can manipulate the data values of variables within the calling functions. This is exactly what we have done with the scanf() function. scanf( “%d”, &ivar ); A.Honey (Nov2003) S3-31

  32. Pointers! One of the primary reasons for pointers is to be able to ‘pass by reference’ parameters into functions, so that those functions can manipulate the data values of variables within the calling functions. This is exactly what we have done with the scanf() function. scanf( “%d”, &ivar ); The above statement passed ivar by reference so that the scanf() function can get an integer value from the user, via the keyboard, and return that value in the variable ivar. Thus scanf will manipulate a data value within the function which used scanf. A.Honey (Nov2003) S3-32

  33. Pointers!S3_pgm2 So let’s create a new program which manipulates our xtank by randomly moving it on the screen. Create a function with the following prototype: void weird_joystick( int *xpos, int *ypos ) The function should call the randomize function twice, once for the x value and once for the y position. A.Honey (Nov2003) S3-33

  34. Pointers!S3_pgm2 So let’s create a new program which manipulates our xtank by randomly moving it on the screen. Create a function with the following prototype: void weird_joystick( int *xpos, int *ypos ) The function should call the randomize function twice, once for the x value and once for the y position. Then create main which calls weird_joystick() to get the desired positions and then use the move function (provided) with the following prototype: void move( int xpos, int ypos ) A.Honey (Nov2003) S3-34

  35. Pointers!S3_pgm2 So let’s create a new program which manipulates our xtank by randomly moving it on the screen. Create a function with the following prototype: void weird_joystick( int *xpos, int *ypos ) The function should call the randomize function twice, once for the x value and once for the y position. Then create main which calls weird_joystick() to get the desired positions and then use the move function (provided) with the following prototype: void move( int xpos, int ypos ) Main should contain an infinite while-loop just as we did for the xtank program. A.Honey (Nov2003) S3-35

More Related