[英]Beginner C - trying out functions
I tried to write my first function in ANSI C. 我试着在ANSI C中编写我的第一个函数。
The purpose of the function is to get 2 user inputs ( capital , interest_rate ) and to return the result of 'interest_rate * capital' to the main function in which I then try to print out the final result. 该函数的目的是获得2个用户输入(capital,interest_rate)并将'interest_rate * capital'的结果返回到main函数,然后我尝试打印出最终结果。
My code so far: 我的代码到目前为止:
#include <stdio.h>
/* k= Kapital ( capital )
* i= Zinssatz ( interestrate )
* s= aufruf der compute funktion
*
*/
long compute_interest (long k,long i) {
printf("Bitte geben Sie Ihr Startkapital ein\n"); /*user input capital*/
scanf("%ld\n", &k);
printf("Bitte geben Sie den Zinssatz ein\n"); /*user input intrstrte*/
scanf("%ld\n", &i);
return k * i;
}
long main(void) {
long s;
s = compute_interest;
printf("geld = %ld\n", s);
return 0;
}
Compiling gives me this error message: 编译给我这个错误信息:
warning: assignment makes integer from pointer without a cast
[enabled by default]
s = compute_interest;
^
What is my mistake? 我的错是什么? What should I change? 我应该改变什么?
You need ()
to make a function call: 你需要()
来进行函数调用:
compute_interest()
Additionally, the function takes two arguments, so you need to send them in... for example: 此外,该函数有两个参数,因此您需要将它们发送到...例如:
compute_interest(2000, 2)
Since you are not passing any value to the function, i suggest you leave the function empty. 由于您没有向函数传递任何值,我建议您将该函数保留为空。 ie your function should be like this 即你的功能应该是这样的
long compute_interest () {
long k, i;
printf("Bitte geben Sie Ihr Startkapital ein\n"); /*user input capital*/
scanf("%ld\n", &k);
printf("Bitte geben Sie den Zinssatz ein\n"); /*user input intrstrte*/
scanf("%ld\n", &i);
return k * i;
}
Then to call up the function you have to put the parentheses. 然后调用你必须放括号的函数。 ie 即
long main(void) {
long s;
s = compute_interest();
printf("geld = %ld\n", s);
return 0;
}
This must give you the desired results. 这必须给你想要的结果。 Hope this helps 希望这可以帮助
Two issues: 两个问题:
s = compute_interest;
This doesn't call the function. 这不会调用该函数。 Because you omitted the ()
, this actually tries to assign a pointer to the function to s
. 因为你省略了()
,这实际上试图将一个指向函数的指针赋给s
。 This is why you're getting the warning. 这就是你收到警告的原因。 Do this to call the function: 这样做来调用函数:
s = compute_interest();
Which brings us to the second issue. 这将我们带到第二个问题。 You define compute_interest
to take two parameters, however the value of those parameters is overwritten by the scanf
calls. 您可以定义compute_interest
以获取两个参数,但scanf
调用会覆盖这些参数的值。
What you really want in this situation is for k
and i
to be local variables rather than parameters: 在这种情况下你真正想要的是k
和i
是局部变量而不是参数:
long compute_interest () {
long k, i;
printf("Bitte geben Sie Ihr Startkapital ein\n"); /*user input capital*/
scanf("%ld\n", &k);
printf("Bitte geben Sie den Zinssatz ein\n"); /*user input intrstrte*/
scanf("%ld\n", &i);
return k * i;
}
In C, function names are treated as pointer to the function itself. 在C中,函数名称被视为指向函数本身的指针。 compute_interest
is a function name and its type is long (*)(long, int)
. compute_interest
是一个函数名,其类型为long (*)(long, int)
。 s
is of type long int
and the assignment s = compute_interest;
s
的类型为long int
,赋值为s = compute_interest;
is assigning a pointer to long
data type and that's the reason why you are getting the warning. 正在分配一个指向long
数据类型的指针,这就是你收到警告的原因。
You need to place ()
after compute_interest
to let the compiler know that its a function call. 你需要在compute_interest
之后放置()
让编译器知道它是一个函数调用。 You also need to remove functions parameters long k,long i
and place it inside the function body. 您还需要删除long k,long i
中的函数参数并将其放在函数体内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.