函数的参数传递

来源:互联网 发布:java面试宝典pdf下载 编辑:程序博客网 时间:2024/05/16 07:30

函数的参数传递有两种方式,一是直接传值,二是引用。直接传值是将实参的值传递给函数工作区形参的副本中,在函数的执行过程中,改变的是副本的值,当函数执行完毕返回时,副本将被清除,原实参的值不会改变。当使用引用时,传递的是实参的地址,这时形参相当于实参的别名,函数执行时,直接改变原实参的值。

// aa.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdlib.h>#include <stdio.h>#include <string.h>void swap1(int a,int b);void swap2(int &a,int &b);void swap1(int a,int b)  //传值{int temp;temp=a;a=b;b=temp;}void swap2(int &a,int &b)  //引用{int temp;temp=a;a=b;b=temp;}int main(int argc, char* argv[]){  int m=3,n=5;  swap1(m,n);printf("swap1:m=%d,n=%d",m,n);    printf("\n");  swap2(m,n);    printf("swap2:m=%d,n=%d",m,n);printf("\n"); return 0; }


原创粉丝点击