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 Functions

Shaun Kelly
Shaun Kelly
5,648 Points

functions with char instead of int

My code doesn't work for some reason and it's printing out the letter f which I don't know why because I haven't used the char 'f' in this situation??

char functionOne(char a, char b, char c);
int main () {
    char word = functionOne('h', 'e', 'y');
    printf("%c\n", word);

    return 0;
}


char functionOne(char a, char b, char c){
    return a + b + c;
}

5 Answers

Kyle Adams
Kyle Adams
5,571 Points

I'm thinking maybe it is because the functionOne method is a char itself, which would only be one character. Try changing that to a string? Also word is a character so that would have to be changed to a string. You are passing in several characters and thus you will either need to create an array of characters or make the return value and the type of word to be string

Shaun Kelly
Shaun Kelly
5,648 Points

can you provide with an example on how to do the char array with the function ?

Kyle Adams
Kyle Adams
5,571 Points

ok so I doubt you want to do char array but working with strings would be a bit easier. Change the value of the functionOne to NSString * and you can create a new string in the function with [NSString stringWithFormat:@"%c%c%c", a, b, c]. Then back in main instead of using char word, you would use NSString * word Then also when you are printing out you will want to use %@ instead of %c since you are now using an NSString object

Shaun Kelly
Shaun Kelly
5,648 Points

i'm working in a c file not objective c so I can't use NSString

I'd be interested to know the answer as well if you figure it out! Thanks :-)

Shaun Kelly
Shaun Kelly
5,648 Points

This is the only thing I could come up with but the problem is that it's only carrying one parameter which doesn't solve the problem I stated above.

char function(char a);
int main () {



    printf("%c%c%c\n", function('h'), function('e'), function('y'));


    return 0;
}

char function(char a){

    return a;

}