C ++中的纯函数
对于相同的参数值,纯函数总是返回相同的结果。它们仅返回结果,并且没有诸如参数修改,I/O流,输出生成等额外的副作用。
一些纯函数是sin(),strlen(),sqrt(),max(),pow(),floor()等一些不纯的功能rand(),time()等等。
一些程序来演示一些纯功能,如下所示-
strlen()
该strlen()函数用于查找字符串的长度。以下程序演示了这一点-
示例
#include<iostream>
#include<string.h>
using namespace std;
int main() {
char str[] = "Rainbows are beautiful";
int count = 0;
cout<<"The string is "<< str <<endl;
cout <<"The length of the string is "<<strlen(str);
return 0;
}输出结果
上面程序的输出如下-
The string is Rainbows are beautiful The length of the string is 22
sqrt()
该sqrt()函数用于查找数字的平方根,以下程序对此进行了演示-
示例
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int num = 9;
cout<<"Square root of "<< num <<" is "<<sqrt(num);
return 0;
}输出结果
上面程序的输出如下-
Square root of 9 is 3
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志