The instructor reviews the challenge exercise and points out which parts are ripe for putting into a function and why. Other parts of the code are reviewed and an explanation is offered as to why these are poor candidates for a function.
(upbeat music) … - Here's the challenge file for this exercise. … The first thing that pops out at me … when I see code like this … is how statements repeat. … I hope you saw it here at lines 13,14, … 16,17 and 19,20. … With the exception of the variables dice one, … dice two and dice three, … there's a lot of repetitive code here. … For me, the solution is obvious, … I must write a function … that generates random values, … which can be assigned directly to each … of the three variables, dice one, … dice two and dice three. … Here's my solution … where I wrote the role function, … I pulled a variable r from the main function … and stuck the two lines that repeat right in here. … R is assigned a value … that's generated by the rand function. … Module six reduces the value … to a range between zero and five, … and then r is increased by one … so that you have roles one through six. … The value of variable r is then returned. … In the main function, … the role function is called three times …
- Writing your first C program
- Working the C development cycle
- Adding comments to code for clarity
- C language data types
- Declaring variables
- Making new data types
- Typecasting variables
- Using math, assignment, and logical operators
- Creating for and while loops
- Nesting loops
- Creating functions
- Returning values from functions
- Passing arguments to functions
- Creating recursive functions
Skill Level Intermediate
C: Data Structures, Pointers, and File Systemswith Dan Gookin2h 57m Intermediate
1. The Fundamentals of C
2. Variables and Values
3. Operators and Math
4. Decisions and Loops
Next steps1m 38s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.