我已经习惯了命令式编程,这是告诉计算机逐步执行程序以获得最终结果的常用方法.另一方面,声明性编程只是传递输入并期望输出而不说明过程如何完成.我很困惑的是功能编程.我知道函数式编程是一种编程范式,它将计算视为数学函数的评估,避免使用状态和可变数据,而不是一种声明性语言.但是,我仍然不能理解它是如何工作的.
我们举一个执行Fibonacci数的例子.
命令式编程:
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,c,a=0,b=1;
printf("Enter Fibonacci series of nth term : ");
scanf("%d",&n);
printf("%d %d ",a,b);
for(i=0;i<=(n-3);i++)
{
c=a+b;
a=b;
b=c;
}
printf("%d ",c);
getch();
}
Run Code Online (Sandbox Code Playgroud)
声明性编程:
Give the nth number and it will return the value of the nth number
Run Code Online (Sandbox Code Playgroud)
功能计划如何运作?
如果我的定义错了,请纠正我.请随意发表评论..
terminology ×1