Date:
2014-1-9
Summary:
函数参数相关记录
Contents:
1.形参实参
形参:用于接收值的变量被称为形参
实参:传递给函数的值被称为实参
2.函数的参数传递之后2种
a).值传递.将实参的值拷贝之后,传递给形参(如果是指针,则拷贝指针的值,传递给形参)
b).引用传递.如果形参是引用,则形参被绑定到了对应的实参上
Notes:
1.使用引用作为形参,尽量避免拷贝
2.函数参数,非引用传递,则为拷贝值传递
EX:
//
// main.cpp
// Cpp_All_Test
//
// Created by StephenLee on 1/1/14.
// Copyright (c) 2014 StephenLee. All rights reserved.
// void mySwap(int* pNum1,int* pNum2)
{
int temp = (*pNum1);
(*pNum1) = (*pNum2);
(*pNum2) = temp;
} void mySwap(int& num1,int& num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
} int nNum1 = ;
int nNum2 = ;
cout << "nNum1:" << nNum1 << "\nnNum2:" << nNum2 << endl;
mySwap(&nNum1,&nNum2); cout << "after mySwap \nnNum1:" << nNum1 << "\nnNum2:" << nNum2 << endl; mySwap(nNum1,nNum2);
cout << "after mySwap \nnNum1:" << nNum1 << "\nnNum2:" << nNum2 << endl;
THE END