-6

main()から別の関数に変数を渡す方法がわかりません。私はこのようなものを持っています:

main()
{
  float a, b, c;

  printf("Enter the values of 'a','b' and 'c':");
  scanf("%f %f %f",&a,&b,&c);
}

double my_function(float a,float b,float c)
{
  double d;

      d=a+b+c
      bla bla bla bla

mainからmy_functionにa、b、cを渡すにはどうすればよいですか?今のところ、プログラムはscanf()で停止し、値を入力した直後に終了します。

ここでさまざまな例を見てきましたが、あまり役に立ちませんでした。

4

3 に答える 3

5

aパラメータ、、、bを渡して関数を呼び出すだけcです。構文:

retval = function_name(parameter1,parameter2,parameter3); //pass parameters as required

このような:

int main(void)
{
    float a, b, c;
    double d;

    printf("Enter the values of 'a','b' and 'c': ");
    if (scanf("%f %f %f",&a,&b,&c) == 3)
    {
        d = my_function(a, b, c);
        printf("Result: %f\n", d);
    }
    else
        printf("Oops: I didn't understand what you typed\n");      
}
于 2012-11-18T17:26:15.110 に答える
2

関数呼び出し。

my_function(a, b, c);
于 2012-11-18T17:24:38.650 に答える
2

mainから関数を呼び出す必要があります!

float my_function(float a,float b,float c)
{
  float d;

  d=a+b+c;
  return d ;
}

int main()
{
  float a, b, c;
  float result ;

  printf("Enter the values of 'a','b' and 'c':");
  scanf("%f %f %f",&a,&b,&c);

  result = my_function(a,b,c);
  printf("\nResult is %f", result );    

  return 0;
}
于 2012-11-18T17:30:00.107 に答える