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

Alastair Halliday
Alastair Halliday
249 Points

dont understand basic function declaration

Im sorry to say that I'm having trouble even executing a basic function for some reason. When I try this it prints nothing. (outside of main())

void addTwo(){
   // float alpha = 1.11;
    float beta = 2.22;
    //float result = alpha + beta;
    printf("result is %f\n", beta);
}
Alastair Halliday
Alastair Halliday
249 Points

wow this formatting is messy. I guess I don't even know how to format code in a question… this is going to be a steep learning curve :)

Stone Preston
Stone Preston
42,016 Points

take a look at the tips for asking questions video on the right side of the page to see how to format your code correctly. You can also take a look at this forum post. I edited your post for you so you can see how to format a code block if you edit your post

4 Answers

Alastair Halliday
Alastair Halliday
249 Points

Okay, thanks for clarifying. I get it now. I think the scope video confused me followed directly by the quiz.

I agree I spent a long time on this and had about 5 different code formats that I copy and pasted it into xCode and the build was successful and it printed, addTwo 1.3. Since my floats were 1.1 and 1.2. I spent hours because I didn't want to cheat and look at the forums when xCode successfully gave me a addTwo 1.3 result, many different ways. I feel lost but I did more than I had to I guess.

Stone Preston
Stone Preston
42,016 Points

the challenge asks you only to implement the function. All its supposed to do is add 2 numbers together and return them, not print anything out. Your function is incorrect because it excepts no arguments and doesnt return anything (printing and returning are 2 different things). It is supposed to accept 2 float arguments and return the sum of those arguements (hint: your return type should be float). Here is a shell of what your function should look like:

returnType functionName(dataType argumentOne, dataType argumentTwo) {

return something;

}

your function needs to return the sum of the 2 variables passed in as arguments

Alastair Halliday
Alastair Halliday
249 Points

Thanks Stone. I was using print because I wanted to get SOME kind of output to see what the function was doing and how it even works. I guess my question is two fold then -

1) how do I actually get the function to "go" not just set it up.

2) can't for the life of me figure this thing out.

float addTwo();
float alpha = 1.11;
float beta = 2.22;
float addTwo(alpha, beta){
    return alpha + beta;
}
Stone Preston
Stone Preston
42,016 Points

to pass the challenge the only thing you need to do is set it up. If you try calling the function (making the function go) you will not pass. If you wanted to try this in xcode you would need to define your function outside of the main function and then you can call it (make it go) and print out the result using something like this

//function prototype is necessary if we call the function before we define it
float addTwo(float a, float b);

//main function
int main() {

//create a float variable and assign it the return value of our addTwo function call
float sum = addTwo(1, 4);

//print the sum out
printf("sum: %d", sum);

}

//definition of addTwo function 
float addTwo(alpha, beta){
    return alpha + beta;
}