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

Louis Darby
Louis Darby
5,149 Points

Having trouble with the functions challenge at the end of functional programming in Objective C.

I'm finding it quite difficult to establish what needs to be written. Any pointers to help me out?

2 Answers

Stone Preston
Stone Preston
42,016 Points

the challenge states: 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.)

so this is a shell of what your function should look like

returnType functionName(dataType argumentOne, dataType argumentTwo) {

    return something;

}

the challenge tells you what to name it, the dataTypes of the arguments, and what to return. you can name the arguments whatever you want, as long as they are valid variable names.

here is a hint: it tells you should return the sum of the arguments passed in to the function, so the return statement should incorporate a plus sign

Louis Darby
Louis Darby
5,149 Points

Thank you for your response. What I had written down based on what I took from the tutorial was:

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

Do I not need to put float at the start even if the numbers within it are going to be float values?

Also when trying to print the result of this in the code block for some reason I get a massive number?

What I wrote was:

printf("%f",addTwo);

Sorry if it's a silly question! Thank you for your help

Stone Preston
Stone Preston
42,016 Points

you only need to write

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

to pass the challenge. if you add anything else it wont pass. dont print anything out. you only need to write the dataType of variables the first time you declare them. so you only need float in front of a and b where they are inside the parenthesis.

however, if you did want to print something out, you would need to pass in arguments to the function

printf("%f",addTwo(1,5));

would print out 6 to the console

Louis Darby
Louis Darby
5,149 Points

Great, thanks so much for the help! I was overcomplicating it!

Stone Preston
Stone Preston
42,016 Points

no problem, glad I could help

Louis Darby
Louis Darby
5,149 Points

Thank you for your response. What I had written down based on what I took from the tutorial was:

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

Do I not need to put float at the start even if the numbers within it are going to be float values?

Also when trying to print the result of this in the code block for some reason I get a massive number?

What I wrote was:

printf("%f",addTwo);

Sorry if it's a silly question! Thank you for your help