Home > Mobile >  Function with variables in C
Function with variables in C

Time:10-29

Maybe a stupid question, but I don't understood why my program in C works with integers, but not with float.

#include <stdio.h>

main()
{
  float a, b;
  a = 4.5;
  b = 9.6;

  printf("Result of %.2f   %.2f = %.2f", a, b, added_up(a, b));
}

/****************************************/

added_up(a,b)
{
  float c;
  printf("Value a: %.2f \n", a);
  printf("Value b: %.2f \n", b);
    c = a   b;
  return (c);
}

My output:

Value a: 0.00
Value b: 0.00
Result of 4.50   9.60 = 0.00

If I define the float to int the program works. What's the failure? Should I declare the function as float, but if I try I get another failure.

CodePudding user response:

  1. You must declare a function before using it

  2. You have to define what type the function returns and you must define type of arguments you are passing to the function


#include <stdio.h>
float added_up(float a,float b); // 1)
int main()
{
  float a, b;
  a = 4.5;
  b = 9.6;

  printf("Result of %.2f   %.2f = %.2f", a, b, added_up(a, b));
  return 0;
}

/****************************************/

float added_up(float a,float b)// 2)
{
  float c=a b;
  printf("Value a: %.2f \n", a);
  printf("Value b: %.2f \n", b);
  return c;
}
  • Related