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

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