Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将参数传递到函数中_C_Function_Arguments - Fatal编程技术网

C 将参数传递到函数中

C 将参数传递到函数中,c,function,arguments,C,Function,Arguments,编写一个C程序来读取卢比金额(整数值)并将其打破 尽可能少地使用纸币。 假设纸币的面额为2000、500、200、100、50、20 和10 我试图将amount传递给denCAL()函数,并希望在每次调用该函数时更新它,但amount的值保持不变 请为我的问题提供解决方案和更好的解决方法,并让我知道这里缺少的一些好的编程实践 #include <stdio.h> int amount, note, den; int denCAL(amount, den){ note = amoun

编写一个C程序来读取卢比金额(整数值)并将其打破 尽可能少地使用纸币。 假设纸币的面额为2000、500、200、100、50、20 和10

我试图将amount传递给denCAL()函数,并希望在每次调用该函数时更新它,但amount的值保持不变

请为我的问题提供解决方案和更好的解决方法,并让我知道这里缺少的一些好的编程实践

#include <stdio.h>
int amount, note, den;
int denCAL(amount, den){
note = amount/den;
amount -= note*den;
printf("Number of %d notes are:%d\n",     den, note);
}
int notes(){
printf("Enter the amount in rupees: ");
scanf("%d", &amount);
if(amount >= 2000){
    denCAL(amount, 2000);
}
if(amount >= 500){
    denCAL(amount, 1000);
}
if(amount >= 200){
    denCAL(amount, 500);
}
if(amount >= 100){
    denCAL(amount, 100);
}
if(amount >= 50){
    denCAL(amount, 50);
}
if(amount >= 20){
    denCAL(amount, 20);
}
if(amount >= 10){
    denCAL(amount, 10);
}
}
int main(){
 notes();
}
但在代码中,您需要:

添加
return
语句:

int denCAL(amount, den)
{
    int note = amount/den;
    amount -= note*den;
    printf("Number of %d notes are:%d\n",     den, note);
    return amount;
}
然后在每一个if变化
denCAL(金额,…)
amount=denCAL(金额,…)

之所以不更新
金额
变量,即使考虑到它是全局变量,也是因为在函数
denCAL()
中,通过将
金额
作为参数传递,您正在访问它的副本。此外,还需要在函数原型中指定输入参数的类型。您应该像这样更改代码:

void denCAL(int den){ // amount is global, no need to pass it
note = amount/den;
amount -= note*den;
printf("Number of %d notes are:%d\n",     den, note);
}

void notes(){
    printf("Enter the amount in rupees: ");
    scanf("%d", &amount);
    if(amount >= 2000){
        denCAL(2000);
    }
    /* rest of code */
}

您可以使用的另一种方法,而不是使用全局变量,例如使用指针。您可以在
notes()
中定义
amount
,并删除已声明的全局值


void denCAL(int* amount, int den){ // passing amount by ADDRESS (so it gets modified)
note = (*amount)/den;
(*amount) -= note*den;
printf("Number of %d notes are:%d\n",     den, note);
}

void notes(){
    int amount;
    printf("Enter the amount in rupees: ");
    scanf("%d", &amount);
    if(amount >= 2000){
        denCAL(&amount, 2000);
    }
    /* rest of code */
}


“想在每次调用函数时更新它”-有多种方法可以做到这一点,包括全局变量、静态局部变量、按地址传递和返回更新值(与未使用的结果类型
denCAL
)。任何一种方法都可以解决您的问题,而且每种方法都不同。我建议学习按地址传递(这实际上只是一个传递值,但这种情况下的值是一个地址;这就是语言的工作方式)。
[c]在这个网站的搜索框中传递地址将产生数百个可能的答案来帮助你。阅读警告通过引用传递并在副作用中转达应该是“最后手段”。对初学者来说,这是一个糟糕的例子。你也不阅读警告(更糟糕的是)。
void denCAL(int den){ // amount is global, no need to pass it
note = amount/den;
amount -= note*den;
printf("Number of %d notes are:%d\n",     den, note);
}

void notes(){
    printf("Enter the amount in rupees: ");
    scanf("%d", &amount);
    if(amount >= 2000){
        denCAL(2000);
    }
    /* rest of code */
}


void denCAL(int* amount, int den){ // passing amount by ADDRESS (so it gets modified)
note = (*amount)/den;
(*amount) -= note*den;
printf("Number of %d notes are:%d\n",     den, note);
}

void notes(){
    int amount;
    printf("Enter the amount in rupees: ");
    scanf("%d", &amount);
    if(amount >= 2000){
        denCAL(&amount, 2000);
    }
    /* rest of code */
}