From the course: C Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Returning a value

Solution: Returning a value - C Tutorial

From the course: C Essential Training

Start my 1-month free trial

Solution: Returning a value

(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…

Contents