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

where am i going wrong for the codechallenge question of functions ?

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

functions.c
int add_two ( float A , float B)
{
  float ac = 2.5;
  float bc = 3.6;
  printf("add the two %d", add_two (float (ac), float (bc)));
  return float A + B;

  }

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

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

Hey there! First of all, lets step through your code

int add_two ( float A , float B) {  // 1.
  float ac = 2.5; // 2.
  float bc = 3.6; // 2.
  printf("add the two %d", add_two (float (ac), float (bc))); // 2.
  return float A + B; // 3.
}
  1. You are asked to return a float, but you are returning an int. You are asked to name the function addTwo, but you are naming it add_two. Moreover, please don't use uppercase characters for variable and constant names, this is reserved exclusively for classes, structs and enums.
  2. This code is not required, please remove it.
  3. The float in the return statement is not correct, you are already specifying the return before your function name. This is invalid syntax and will prevent your code from compiling.

Applying these changes, you should end up with the following code

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

Hope that helps :)