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

Devin Scheu
Devin Scheu
66,191 Points

Help with functions

Here's the question : Implement a function named "addTwo" that returns the sum of two floats. The function will accept two float numbers as arguments. It should add the two arguments together, and return the result. (No need to write the main function. Just write out the implementation for the addTwo function.)

Here's my code:

int addTwo;

float a = 5;

float b = 3;

addTwo {

return a + b;

}

2 Answers

Stone Preston
Stone Preston
42,016 Points

you have the right idea. remove the first part of your code so that you are just left with

addTwo {

return a + b;

}

now you are going to have to make some changes to that piece of code. your functions needs a return type before the name. it also needs to accept 2 arguments that go in parenthesis after the name. (hint, go head and call them float a and float b). if you make those changes your function will be correct. your return statement is perfect so dont change that.

here is a shell of what your function should look like:

returnType functionName(dataType argumentOne, dataType argumentTwo) {

return something;

}

the challenge tells you what the return type should be (float, not int), the function name, and the datatypes of the arguments (also floats). you can call the arguments (actually they are known as parameters in this case) whatever you want, just be consistent when referencing them. it says to return the sum of the arguments passed in to the function

Devin Scheu
Devin Scheu
66,191 Points

Thanks, stone. I believe that this is the correct function.

float addTwo (float a, float b) {
  return a + b;
}