####function主体结构 function.name <- function(arguments) { computations on the arguments some other code }
` mycal<-function(x){ mean<-mean(x) sd<-sd(x) result <- list(mean=mean,sd=sd) return(result) } x<-c(1,2,3,80) mycal(x) mysave<-function(earn,spend,lost){ save<- earn-spend-lost result <- list(save=save) return(result) } mysave(earn = 1000,spend = 80,lost = 10) myscore<-function(paper,attendence){ score<- paper*0.7+attendence*.3 result <- list(score=score) return(result) } myscore(paper = 92,attendence = 70)
函数式编程语言不同于大多数人所熟知的编程范式,它没有了维护全局状态的麻烦,只需要将输入数据传给函数,然后等待输出结果,就这么简单。
Simon 说,“大部分编程语言都是命令式(imperative)的,程序员需要告诉代码先做什么再做什么。而函数式编程语言则不然,它会直接告诉程序员输出的结果是什么。比如 Excel 表格里的方程式,它并不包含一系列的步骤,它只会告诉你某个格子经过计算之后的值是多少”。
|