Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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. S3-1A.Honey (Nov2003)

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. S3-2A.Honey (Nov2003)

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. S3-3A.Honey (Nov2003)

4 Function return type Each function also has a type, which is referred to as the functions ‘return type’. 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’. S3-4A.Honey (Nov2003)

5 Function return type Each function also has a type, which is referred to as the functions ‘return type’. 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 return statement 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. S3-5A.Honey (Nov2003)

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. S3-6A.Honey (Nov2003)

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. S3-7A.Honey (Nov2003)

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. S3-8A.Honey (Nov2003)

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. S3-9A.Honey (Nov2003)

10 Creating a function S3_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) S3-10A.Honey (Nov2003)

11 Creating a function S3_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. S3-11A.Honey (Nov2003)

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

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

14 More functions xtank Let’s build an xtank program. What is an xtank you ask? An xtank program 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. S3-14A.Honey (Nov2003)

15 More functions xtank 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! S3-15A.Honey (Nov2003)

16 More functions xtank 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. S3-16A.Honey (Nov2003)

17 More functions xtank 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. S3-17A.Honey (Nov2003)

18 More functions xtank 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! S3-18A.Honey (Nov2003)

19 More functions xtank 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(). S3-19A.Honey (Nov2003)

20 More functions xtank 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 ); } S3-20A.Honey (Nov2003)

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

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: * ; Such as: int *ivar; float *fvar; S3-22A.Honey (Nov2003)

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: * ; 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. S3-23A.Honey (Nov2003)

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

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 ); S3-25A.Honey (Nov2003)

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. S3-26A.Honey (Nov2003)

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

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); S3-28A.Honey (Nov2003)

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? S3-29A.Honey (Nov2003)

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! S3-30A.Honey (Nov2003)

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 ); S3-31A.Honey (Nov2003)

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. S3-32A.Honey (Nov2003)

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. S3-33A.Honey (Nov2003)

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 ) S3-34A.Honey (Nov2003)

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. S3-35A.Honey (Nov2003)


Download ppt "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."

Similar presentations


Ads by Google