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 - C Tutorial
From the course: C Essential Training
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
Understanding functions3m 21s
-
(Locked)
Creating a function3m 28s
-
(Locked)
Challenge: Writing a function46s
-
(Locked)
Solution: Writing a function1m 10s
-
(Locked)
Returning a value from a function2m 33s
-
(Locked)
Challenge: Returning a value42s
-
(Locked)
Solution: Returning a value1m 40s
-
(Locked)
Passing arguments to a function1m 52s
-
(Locked)
Challenge: Passing values44s
-
(Locked)
Solution: Passing values1m 22s
-
(Locked)
Using the main() function's arguments3m 30s
-
(Locked)
Retaining values in a function2m 58s
-
(Locked)
Creating recursive functions3m 53s
-
(Locked)
Challenge: Complete the code1m 14s
-
(Locked)
Solution: Complete the code1m 31s
-
-