Download presentation
Presentation is loading. Please wait.
Published byChester Webster Modified over 7 years ago
1
ECE 382 Lesson 20 Lesson Outline Structs Functions Headers Example
Admin Assignment 6 due BOC today Lab 3 Notebook Due COB today Assignment 7 due BOC next lesson
2
Example Code from Assignment 6
Good, Bad, or Ugly? Student Example 1 Student Example 2 Student Example 3
3
C Language: Structs Struct: similar to an “object” from Java, but do not have their own “methods” struct <name> { <type> <var1>; <type> <var2>; ... }; struct <name> <variable_name>; <variable_name>.<var1> = <value>; <variable_name>.<var2> = <value>; struct point { char x, y; }; struct circle { struct point center; char radius; void main(void) { // create and initialize like this: struct point myCenter = {20, 7}; struct circle myCircle = {myCenter, 5}; // dot notation to access variables: myCenter.x = 10; myCircle.radius = 25; myCircle.center.x = 12; }
4
C Language: Functions Functions: like subroutines in assembly or methods in Java. Remember Modularity? Example Function Call void main(void) { unsigned int mySummation; unsigned char maxN = 42; mySummation = summation(23); mySummation = summation(maxN); } How does the compiler know what “summation” is?
5
C Language: Functions How does summation work? Function Prototype
Promises the compiler that the function is implemented elsewhere Provides the “interface” to the function You are allowed to "call" the function from your code The function prototype must be defined in a location physically before you call it (i.e. defined above main() or in a #include file). If you offer a prototype but don't provide an implementation, you'll get a linker error. <output_type> func_name(<input type 1> <variable name 1>, ...); // Header // #includes // #defines unsigned int summation (unsigned char n); void main(void) { unsigned int mySummation; unsigned char maxN = 42; mySummation = summation(23); mySummation = summation(maxN); } How does summation work?
6
C Language: Functions Function Definition
Can be with the prototype, or appear later in the code <output_type> func_name(<input type 1> <variable name 1>, ...) { // Some interesting stuff return <output variable>; } unsigned int summation (unsigned char n); void main(void) { unsigned int mySummation; unsigned char maxN = 42; mySummation = summation(23); mySummation = summation(maxN); } unsigned int summation(unsigned char n) // recursion! if (n <= 0) return 0; else return n + summation(n-1);
7
C Language: Preprocessor Commands
Preprocessor Commands: The preprocessor is executed before your code compiles #include "file_name.h" Essentially a "copy and paste" of the file_name.h into your file "file_name.h", the preprocessor will search in your project working directory <file_name.h>, the preprocessor will search your class path. #define <SINGLE_WORD> <replacement_token> Essentially a global "search and replace" within your code Anytime the <SINGLE_WORD> token appears, it will be replaced by the <replacement token> #ifndef <SOME_CONSTANT> ... <some code> ... #endif Code is only included if <SOME_CONSTANT> is not defined Usually, your first line of code will be to #define <SOME_CONSTANT> typedef unsigned short int16; Create your own type
8
C Header files A separate file that contains a related set of: Function prototypes typedef declarations #define constants etc. File naming convention: All lowercase Use "_" to combine words ".h" is the file extension Example: atd_helper.h You must "wrap" the header in a #ifndef to prevent circular inclusions (also called guarding) #ifndef _ATD_HELPER_H_ #define _ATD_HELPER_H_ // Your header file code (typedefs, function prototypes, #defines, etc.) // Use good comment headers to define each function (see example) // ... #endif // _ATD_HELPER_H
9
C Implementation Files
A separate C file that implements the header file Contains the function definitions #include the header file as your first line File naming convention: Same name as the header file! ".c" is the file extension Example: atd_helper.c #include "atd_helper.h" // Function definitions // ...
10
In class programming Program Requirements Build a program that can calculate the following: Summation Factorial Minimum of two values Maximum of two values Provides user helpful mathematic constants Must write modular / reusable code: Header file Implementation file main() file
11
main.c #include "math_helper.h" void main(void) { char a = 10; char b = 15; char maxVar, minVar; unsigned int sum, fact; sum = summation(a); fact = factorial(6); maxVar = max(a, b); minVar = min(a, b); while(1); // trap CPU }
12
math_helper.h // Your high-quality header with author / description / revision history #ifndef _MATH_HELPER_H_ #define _MATH_HELPER_H_ // Useful constants // (estimate) (actual) #define PI (339 / 108) // vs #define E (155 / 57) // vs // Note: you would add some really good headers before each of these // functions to describe their purpose. In the interest of // brevity, I'm omitting them here. unsigned int summation(unsigned char n); unsigned int factorial(unsigned char n); char max(char a, char b); char min(char a, char b); #endif // _MATH_HELPER_H_
13
math_helper.c #include "math_helper.h" unsigned int summation(unsigned char n) { if (n <= 0) return 0; else return n + summation(n-1); } unsigned int factorial(unsigned char n) return 1; return n * factorial(n-1); char max(char a, char b) return (a > b) ? a : b; char min(char a, char b) return (a < b) ? a : b;
14
Headers !!!! /* Name:<Your Name> Date:<The date you stated working on the file> Course: <The course's name> File:<This file's name> HW: <HW# and name> Purp:A brief description of what this program does and the general solution strategy. Doc: <list the names of the people who you helped> <list the names of the people who assisted you> Academic Integrity Statement: I certify that, while others may have assisted me in brain storming, debugging and validating this program, the program itself is my own work. I understand that submitting code which is the work of other individuals is a violation of the honor code. I also understand that if I knowingly give my original work to another individual is also a violation of the honor code. */ Want to edit your default empty C template??? Then modify: C:\ti\ccsv6\eclipse\plugins\com.ti.ccstudio.project.templates_ \resources\msp430\empty\main.c Try Assignment 6:
15
Assignment 7 Try Assignment 7: Assignment - Pong!
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.