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
Alex Parvis
4,464 PointsHaving trouble answering quiz on objective C
'Implement a function named "addTwo" that returns the sum of two floats. The function will accept two float numbers as arguments and return a float which is the sum of the two arguments passed to the function. (No need to write the main function just write out the implmentation for the addTwo function)'
Really stuck with this one I'm completely lost, can anyone help me? What is a 'float'? And how do i represent them as 'arguments'.
thanks, Alex
3 Answers
Hasan Pektas
2,128 PointsFirst of, float is a datatype like any other available (like Int, Char, Double etc.).
Int = Integer (e.g. 4 or 10)
Float = Floating number up to 7 decimals (e.g. 1.0054 or 10.55555)
Double = Is also a floating number but up to 2 times larger: up to 15 or 16 decimals. (e.g. 1.5353415335)
Char = A Character (e.g. 'a' or 'G').
These are the most popular datatypes.
What you want to do in some programming languages like Objective-C in this case; is to tell the compiler what type the variable will have. If you're going to give the variable x = 1 then you must tell the compiler before you do that. You will need to change to declaration to: int x = 1. The same rules follows with the float and other datatypes.
An argument to a function is like Math in high-school. (example):
f(x) = 5x + 1.
or
f(x,y) = x + y.
The last function take two arguments: x and y.
The same logic is used in programming except that you tell the compiler what type the function will return.
f(x,y) = x + y
Will be in C-programming if you use Integers:
int f(int x, int y) { return x + y }
Your answer will probably be:
(in C):
float addTwo(float first, float second) {
return first + second;
}
(in Objective-C):
- (float)addTwo:(float)first secondArgument:(float)second {
return first + second;
}
I don't know which one they're asking for.
hassanboulhilt
5,775 Pointsthe same way, you can first declare variable in your first line and in the second you implement your function. sorry about my English but the code is correct
float addTwo (float a, float b); float addTwo (float a, float b) { return a +b; }
Alex Parvis
4,464 PointsThankyou very much guys! I understand this a lot better now!