函数定义可以将参数传递给另一个函数定义而不返回到 C 中的主函数吗?

标签 c function

此代码不起作用。你们有什么方法吗

Pass argument from a Function Definition to another Function Definition without returning back to main functions ?

如果可能的话尽量不要将 main 函数移到末尾。

#include <stdio.h>

int test();
int new(int);

int main()
{
    test();

    int num;
    int new; 
    printf("The num is : %d\n", num);
    printf("The new num is : %d\n",new);

 }

int test()
{
     int num;

     printf("Enter a number: ");
     scanf("%d",&num);
     new (num);
     return num;

 }

 int new(int num)

{
    int new;
    new = num*2;
    return new;
}

最佳答案

这段代码有很多问题。不带参数的函数需要 void在参数列表中。 int test()表示该函数采用未指定数量的参数,而 int test(void)意味着该函数不带参数。另外,scanf()返回成功分配的数量,如果用户输入格式错误的输入,该数量可能为 0(或者在罕见的输入失败情况下为 EOF)。您应该始终检查有返回值的函数的返回值。在这种情况下,如果用户输入非数字输入,不这样做并采取相应行动可能会导致未定义的行为。

不过,这些并不是造成您问题的原因。首先,当从函数返回值时,调用代码必须使用该值。代码如下:

new(num);
return num;

其中new()函数返回一个值,对该返回值不执行任何操作。

另一个问题是变量范围。在 main()函数,例如numnew在任何其他函数中都不可见,类似地,变量 numnew属于其他函数的内容在 main() 中不可见。这是在不同函数中使用相同变量名的缺点;它可能会引起困惑。

main()函数,test()被调用,但不使用返回值。然后numnew被宣布。但这些变量未初始化,因此具有不确定的值。尝试打印这些值会导致未定义的行为。

这是第一个修复:

#include <stdio.h>

int test(void);
int new(int);

int main(void)
{
    int new = test(); 
    printf("The new num is : %d\n", new);

}

int test(void)
{
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);
    return new(num);
}

int new(int num)
{
    int new;
    printf("The num is : %d\n", num);
    new = num * 2;
    return new;
}

这里原始数字的值被打印在 new() 中函数,并将数字的新值传回 test()通过new()函数,然后返回main()通过test()功能。

如果你想将两个值传回main() ,你运气不好。 C 中的函数只能返回一个值。但是,还有其他选项。

您可以使用一个指针或两个指针来获取有趣的值。这些指针将允许函数修改 main() 中已知的值。无需返回任何值。请记住,C 按值传递函数参数,这意味着函数中使用原始参数值的副本。这意味着您对该值所做的任何更改都不会反射(reflect)在原始值中。但是通过传递一个指向值的指针,您可以更改原始值,因为您有一个指向原始值的指针(尽管只是指针值的副本):

#include <stdio.h>

void test(int *old_val, int *new_val);
void update(int *old_val, int *new_val);

int main(void)
{
    int old_val;
    int new_val;

    test(&old_val, &new_val);
    printf("The num is : %d\n", old_val);
    printf("The new num is : %d\n", new_val);
}

void test(int *old_val, int *new_val)
{
    printf("Enter a number: ");
    scanf("%d", old_val);
    update(old_val, new_val);
}

void update(int *old_val, int *new_val)
{
    *new_val = *old_val * 2;
}

另一种选择是使用 struct包含两个值,并传递 struct around(或指向 struct 的指针):

#include <stdio.h>

struct values
{
    int old;
    int new;
};

struct values test(struct values x);
struct values update(struct values x);

int main(void)
{
    struct values my_x;

    my_x = test(my_x);

    printf("The num is : %d\n", my_x.old);
    printf("The new num is : %d\n", my_x.new);
}

struct values test(struct values x)
{
    printf("Enter a number: ");
    scanf("%d", &x.old);
    return update(x);
}

struct values update(struct values x)
{
    x.new = x.old * 2;
    return x;
}

另一种可能性是使用全局变量,但我不会在这里讨论。全局变量通常是一个坏主意,更好的程序设计可以避免。

关于函数定义可以将参数传递给另一个函数定义而不返回到 C 中的主函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52237299/

相关文章:

c - C中这个表达式的返回类型是什么?

C#:如何从类方法绑定(bind)到 ListBox DisplayMember 和 ValueMember 结果?

function - 箭头优于函数

java - 从 jQuery 或 Ajax 调用带参数的方法

c - 结构成员指向另一个结构成员

c - 使用信号量的 sleep 理发师

c - C 中动态类型的宏

IOS Swift 2.0 洗牌功能

使用 JavaScript 在 MVC 中调用 C# 函数

将鼠标位置转换为世界位置 OpenGL