函数类型

函数类型 首页 / 函数式入门教程 / 函数类型

函数有两种类型-

  • 预定义函数
  • 用户自定义函数

在本章中,我们将详细讨论函数。

预定义函数

这些是语言内置的用于执行操作的函数,并存储在标准函数库中。

例如C++中的“ Strcat”和Haskell中的“ concat”用于附加两个字符串,C ++中的“ strlen”和Python中的“ len”用于计算字符串长度.

C ++中打印字符串长度

以下程序显示了如何使用C ++打印字符串的长度-

#include <iostream> 
#include <string.h> 
#include <stdio.h> 
using namespace std;  

int main() {     
   char str[20] = "Hello LearnFk"; 
   int len; 
   len = strlen(str); 
   cout<<"String length is: "<<len; 
   return 0; 
} 

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/functional-programming/functional-programming-function-types.html

来源:LearnFk无涯教程网

String length is: 13

Python中打印字符串长度

以下程序显示了如何使用Python(一种函数编程语言)来打印字符串的长度-

str = "Hello LearnFK"; 
print("String length is: ", len(str)) 

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/functional-programming/functional-programming-function-types.html

来源:LearnFk无涯教程网

('String length is: ', 13)

用户定义的函数

用户定义的函数由用户定义以执行特定任务,有四种不同的模式来定义函数-

  • 没有参数也没有返回值的函数
  • 没有参数但返回值的函数
  • 有参数但无返回值的函数
  • 带参数和返回值的函数

无参数无返回值函数

以下程序显示了如何在 C ++ 中定义没有参数且没有返回值的函数-

#include <iostream> 
using namespace std; 

void function1() { 
   cout <<"Hello LearnFK"; 
}  
int main() { 
   function1(); 
   return 0; 
} 

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/functional-programming/functional-programming-function-types.html

来源:LearnFk无涯教程网

Hello LearnFK 

以下程序显示了如何在 Python 中定义类似的函数(无参数且无返回值)-

def function1():    
   print ("Hello LearnFk") 
    
function1() 

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/functional-programming/functional-programming-function-types.html

来源:LearnFk无涯教程网

Hello LearnFK 

无参数有返回值函数

以下程序显示了如何在 C ++ 中定义不带参数但返回值的函数-

#include <iostream> 
using namespace std; 
string function1() { 
   return("Hello LearnFK"); 
}  

int main() { 
   cout<<function1(); 
   return 0; 
}

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/functional-programming/functional-programming-function-types.html

来源:LearnFk无涯教程网

Hello LearnFK 

以下程序显示了如何在 Python 中定义类似的函数(不带参数,但返回值)-

def function1(): 
   return "Hello LearnFK" 
res = function1() 
print(res) 

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/functional-programming/functional-programming-function-types.html

来源:LearnFk无涯教程网

Hello LearnFK 

有参数无返回值函数

以下程序显示了如何在 C ++ 中定义带参数但没有返回值的函数-

#include <iostream> 
using namespace std; 
void function1(int x, int y) {    
   int c; 
   c = x+y;  
   cout<<"Sum is: "<<c; 
}  

int main() { 
   function1(4,5); 
   return 0; 
}

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/functional-programming/functional-programming-function-types.html

来源:LearnFk无涯教程网

Sum is: 9 

以下程序显示了如何在 Python 中定义类似的函数-

def function1(x,y): 
   c = x + y 
   print("Sum is:",c) 
function1(4,5)

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/functional-programming/functional-programming-function-types.html

来源:LearnFk无涯教程网

('Sum is:', 9)

有参数有返回值函数

以下程序显示了如何在C ++中定义一个不带参数但返回值的函数-

#include <iostream> 
using namespace std; 
int function1(int x, int y) {    
   int c; 
   c = x + y;  
   return c;    
} 

int main() {  
   int res; 
   res = function1(4,5); 
   cout<<"Sum is: "<<res; 
   return 0; 
}

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/functional-programming/functional-programming-function-types.html

来源:LearnFk无涯教程网

Sum is: 9 

以下程序显示了如何在 Python 中定义类似的函数(带有参数和返回值)-

def function1(x,y): 
   c = x + y 
   return c  

res = function1(4,5) 
print("Sum is ",res) 

它将产生以下输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/functional-programming/functional-programming-function-types.html

来源:LearnFk无涯教程网

('Sum is ', 9)

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学微服务 -〔胡忠想〕

白话法律42讲 -〔周甲徳〕

零基础学Java -〔臧萌〕

OpenResty从入门到实战 -〔温铭〕

Vim 实用技巧必知必会 -〔吴咏炜〕

Linux内核技术实战课 -〔邵亚方〕

体验设计案例课 -〔炒炒〕

说透区块链 -〔自游〕

手把手带你搭建推荐系统 -〔黄鸿波〕

好记忆不如烂笔头。留下您的足迹吧 :)