From the course: Complete Guide to C Programming Foundations
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Returning a value from a function - C Tutorial
From the course: Complete Guide to C Programming Foundations
Returning a value from a function
- [Instructor] Functions return of value thanks to the return keyword. The value specified must match the function's data type. For the randchar function shown in this code, the return statement sends back the value of character r. The variable itself isn't returned. Variable r is local or private to the randchar function, but its value is sent back to the main function where a loop outputs the value seven times. The goal is to generate a random word, and today's random word is XQCARWF. Well, that's how I'd pronounce it. The randchar function generates a random value from zero to 25 at line nine. The value of character A is added to the value in variable r at line 10, which produces an uppercase letter of the alphabet. Then the value of variable r is returned. The parentheses here are optional, unless the expression you're returning requires them. Functions in C cannot return more than a single value. Two options for getting around this limitation are using pointers as arguments and…
Contents
-
-
-
-
-
-
-
Understanding functions3m 11s
-
(Locked)
Creating a function4m 52s
-
(Locked)
Challenge: Writing a function39s
-
(Locked)
Solution: Writing a function1m 49s
-
(Locked)
Returning a value from a function2m 23s
-
(Locked)
Challenge: Returning a value53s
-
(Locked)
Solution: Returning a value2m
-
(Locked)
Passing arguments to a function2m 22s
-
(Locked)
Challenge: Passing values41s
-
(Locked)
Solution: Passing values1m 50s
-
(Locked)
Using the main() function's arguments2m 13s
-
(Locked)
Retaining values in a function2m 34s
-
(Locked)
Creating recursive functions3m 44s
-
(Locked)
Chapter challenge: Write the functions1m 14s
-
(Locked)
Chapter solution: Write the functions2m 15s
-
-
-
-
-
-
-