1.函数基础
1.1 函数定义
#include <iostream>
using namespace std;
int main(int argc, const char * argv[])
{
return 0;
}
/*
void functionName(parameterList)
{
statement(s)
return; // 可选的
}
typeName functionName(parameterList)
{
statements
return value; // 必须使用return返回,类型必须兼容typeName
}
*/
void process1(int value) // 返回值类型是void,return返回值可有可无
{
cout << value << endl;
return;
}
int add(int a,int b) // 必须有返回值
{
if(a > b)
{
return a + b;
}
else
{
return 0;
int i = 0; // return之后,后面的语句不会再执行
}
}
1.2 函数调用
#include <iostream>
using namespace std;
void process1(int value); // 函数原型
int add(int a,int b); //
int main(int argc, const char * argv[])
{
process1(20);
add(30,20);
return 0;
}
void process1(int value)
{
cout << value << endl;
return;
}
int add(int a,int b)
{
if(a > b)
{
return a + b;
}
else
{
return 0;
}
}
2.函数参数与值传递
函数与数组类型参数
#include <iostream>
using namespace std;
void initArray(int intArray[], int size);
int main(int argc, const char * argv[])
{
int intArray[10];
int size = sizeof(intArray)/sizeof(int);
cout << size << endl;
initArray(intArray, size);
for(int i = 0; i < size;i++)
{
cout << intArray[i] << " ";
}
cout << endl;
return 0;
}
// 数组作为参数传递给函数后,是一个引用的方式,在函数中对数组进行修改,会影响函数外数组的值
void initArray(int intArray[], int size)
{
for(int i = 0; i < size;i++)
{
intArray[i] = i + 1;
}
}
3.函数与C风格的字符串
将C风格的字符串用作函数参数
#include <iostream>
using namespace std;
void function1(char *str);
void function2(char str[]);
char* repeatChar(char c, int n);
int main(int argc, const char * argv[])
{
char array[] = "hello world";
function1(array);
function2(array);
char c = 'x';
cout << repeatChar(c, 30) << endl; // 返回30个x
return 0;
}
void function1(char *str) // 以指针的方式传C风格字符串
{
cout << str << endl;
}
void function2(char str[]) // 以数组的方式传C风格字符串
{
cout << str << endl;
}
char* repeatChar(char c, int n) // C++中返回值可以是指针类型,但不能是数组类型:char[]
{
char *str = new char[n + 1];
str[n] = '\0';
for(int i = 0; i < n;i++)
{
str[i] = c;
}
return str;
}
4.函数与结构体、共用体
4.1 作为函数参数传入
#include <iostream>
using namespace std;
struct MyStruct
{
int code;
string name;
};
void function1(MyStruct myStruct)
{
myStruct.code = 20;
myStruct.name = "Mike";
}
int main(int argc, const char * argv[]) {
MyStruct myStruct;
myStruct.code = 10;
myStruct.name = "Bill";
function1(myStruct); /* 直接将结构体作为参数传入函数时,在函数中对结构体成员进行修改,并不影响外面的结构体变量 */
cout << "myStruct.code = " << myStruct.code << endl; // 10
cout << "myStruct.name = " << myStruct.name << endl; // Bill
return 0;
}
#include <iostream>
using namespace std;
struct MyStruct
{
int code;
string name;
};
void function2(MyStruct *myStruct)
{
myStruct->code = 40;
myStruct->name = "John";
}
int main(int argc, const char * argv[]) {
MyStruct myStruct;
myStruct.code = 10;
myStruct.name = "Bill";
function2(&myStruct); /* 以指针的形式将结构体传入时,在函数中改变结构体成员,则外面的结构体也会改变 */
cout << "myStruct.code = " << myStruct.code << endl; // 40
cout << "myStruct.name = " << myStruct.name << endl; // John
return 0;
}
4.2 作为函数返回值
#include <iostream>
using namespace std;
struct MyStruct
{
int code;
string name;
};
MyStruct function3() // 返回结构体
{
MyStruct myStruct;
myStruct.code = 100;
return myStruct;
}
MyStruct* function4() // 返回结构体指针
{
MyStruct* myStruct = new MyStruct(); // 用new在堆上分配一个结构体对象大小的内存
myStruct->code = 200;
return myStruct;
}
int main(int argc, const char * argv[]) {
MyStruct myStruct;
myStruct.code = 10;
myStruct.name = "Bill";
cout << "function3().code = " << function3().code << endl; // 100
cout << "function4()->code = " << function4()->code << endl; // 200
return 0;
}
5.函数指针
#include <iostream>
using namespace std;
int factorial(int n);
typedef int (*JC)(int n); /* 函数指针的类型和变量是混在一起的,所以typedef时,也是混着来,以后定义函数指针用JC处理 */
int main(int argc, const char * argv[])
{
int m = factorial(5);
cout << "5的阶乘:" << m << endl; // 120
int (*p_factorial)(int n) = factorial; // 定义了函数指针,指向函数原型
// 必须初始化函数指针
p_factorial = factorial;
cout << "10的阶乘:" << p_factorial(10) << endl; // 3628800
JC jc = p_factorial; // JC是函数指针类型,定义了函数指针jc,并用p_factorial进行初始化
cout << jc(6) << endl; // 720
cout << (*jc)(6) << endl; // 也可以
return 0;
}
// 计算阶乘
int factorial(int n)
{
if(n == 0 || n == 1)
{
return 1;
}
else
{
return factorial(n - 1) * n;
}
}