Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Functional Programming in C Scope

return types

how did the function scope_it_out return a value called "gamma" even though the function 'scope_it_out' is declared as void?

2 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points
void scope_it_out() {
    char bravo[] = "gamma";
    printf("%s\n", bravo);
}

"gamma" was printed on the output console because the printf() function is doing its job, but it's important to remember that printf() and whatever results it generated are NOT the return value, in order for a function to have a return value, the return keyword must be used explicitly.

It's also worth knowing that, when calling a function with return value by itself, nothing will be outputted on the screen unless you pass its return value as argument to a printf() function.

Thanks for the help William.It fixed my doubt.

Hi Surya,

Here is the scope_it_out function definition:

void scope_it_out() {
    char bravo[] = "gamma";
    printf( "%s\n", bravo );
}

Instead of returning a value, a call is made to printf, which prints the contents of the character array bravo. The scope_it_out function itself never returned a value. The call to printf is what caused the string gamma to be displayed.

Thanks Zack for clarifying my doubt.