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

Need help with this concept.

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.)

Thanks

4 Answers

Rashii Henry
Rashii Henry
16,433 Points

float addTwo(float x, float y) { float sum = ( x + y); return sum; }

whenever you're implementing a function(or method in Obj-C) you start with the return type. I started with float. Then you want to name your function, in this case it's addTwo. Right after the name you pass in the parameters separated by commas, make sure before you name those variables you specify the return type. After your done passing in those, open up the curly braces to give this function some instructions. The question wants to return the sum, so i created a new float variable called sum and assigned it the function x + y; After that just return the new variable you just created.

Great, just was I was looking for.

Rashii Henry
Rashii Henry
16,433 Points

what have you tried already? i don't want to just plain out give you the answer.

Jose Estrella
Jose Estrella
1,529 Points

why does the first line below, not have a semi colon?

float addTwo (float x, float y)

Rashii Henry
Rashii Henry
16,433 Points

Good question, but when you're typing a method inside of the implementation you always have to follow it with curly braces to let xCode know that you're about to implement what you want this method to do.

however, inside of the header file. you can just end the line with a semicolon because in the header file you're telling xcode, get ready for this method I'm about to implement.

Jose Estrella
Jose Estrella
1,529 Points

ok got it, thank you your explanation for this challenge really helped me!