使用按值调用交换数字 [英] Swapping of numbers using call by value

查看:60
本文介绍了使用按值调用交换数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/* After executing the below program, i am not getting the desired output, as the digits are not getting swapped even after calling the function */

#include<stdio.h>
int swapv(int x, int y);
int main()
{
   int a = 10, b = 20;

   swapv(a,b);
   printf("\na = %d and b = %d", a,b);
   
   return 0;
}

int swapv(int x, int y)
{
   int t;
   
   t = x;
   x = y;
   y = t;

   printf("\n x = %d and y = %d",  x,y);
}



输出: -


output :-

x = 20 and y =10
a = 10 and b = 20





任何人都可以建议一个正确的方法或解释这个程序。为什么功能没有被正确调用。



我尝试过:



我可以使用指针方法交换值。但为什么我不能直接交换值?



Can anyone please suggest a correct method or explain this program. As why the function is not called properly.

What I have tried:

I can swap the values using pointer method. But why can't i directly swap the values?

推荐答案

正确调用该函数并执行预期的操作。



如果按值传递参数,该函数将使用该值(传递的变量的副本或仅具有常量文字的值)。



如果要将更改的结果返回给调用者,则必须使用指向修改原始变量的指针传递:

The function is called properly and does what is expected.

If you pass parameters by value, the function is working with that value (a copy of a passed variable or just the value with constant literals).

If you want to return changed results to the caller you have to pass by reference using a pointer to modifiy the original variables:
void swapv(int *x, int *y)
{
    int t;
    
    t = *x;
    *x = *y;
    *y = t;
    
    printf("\n x = %d and y = %d", *x, *y);
}

或者,当只返回一个值时,您还可以使用 return 语句返回结果(可以返回多个值)当把它们放入一个结构中但是这样做是非常罕见的C)。



另请注意我已将函数更改为 void 因为它没有返回任何值。

Alternatively you can also return results with the return statement when only one value must be returned (it is possible to return multiple values when putting them into a structure but doing so is rather uncommon with C).

Note also that I have changed the function to be void because it does not return any value.


例如见: C中的值函数调用 [ ^ ]。


Quote:

使用按值调用来交换数字

Swapping of numbers using call by value



这就是问题,一个由值调用的函数正在接收参数值,并且不知道包含这些值的变量或它们来自何处。

And这是合法的:


That is the problem, a function called by value is receiving the values of parameters, and have no idea of the variables that contain those values or where they come from.
And this is legal:

swapv(a, 15+20);



交换的唯一方法调用变量,是通过引用调用,如solution1所示。次要影响前一行是不合法的。


The only way to swap in the calling variables, is to call by references as shown in solution1. Secondary effect the previous line is not legal.

Quote:

为什么函数没有被正确调用。

As why the function is not called properly.



编译器生成一个正确的可执行文件,并且该函数被正确调用,它正在完成它应该做的事情。

简单的按值调用方法不允许您对参数执行的操作传播回调用例程中使用的变量。该函数可以对参数执行所有操作,它对调用变量没有影响。


Compiler produce a proper executable, and the function is called properly, it is doing exactly what it is supposed to do.
Simply a call by value method do not allow what you do on parameters to propagate back to variables used in calling routine. The function can do all what it want on the parameters, it have no effect on calling variables.


这篇关于使用按值调用交换数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆