为什么大多数编程语言被设计成函数只有一个返回值,而不是多个?
程序语言的发展很大的部分受人类正常思维方式的影响,在生活中,给一个输入,有两个输出很常见,而大多数编程语言为什么不这么设计?
—————————————————————
不同编程语言因设计的不同支持1个或多个返回值,故于2016—02—06更改问题。
回复内容:
python允许你干多个返回值的事情。然并卵,其实会包裹成一个tuple,加之tuple本来就可以原素绑定… 其实这个问题也不是一句「返回一个数据结构」就可以清楚解决的。既然返回值可以是一个数据结构,那么参数其实也可以。为什么从C、Pascal这些古老的语言开始,参数就可以是多个,而返回值却是一个呢?这个问题值得探讨一下。假设我们设计了一个语言叫C-,函数的参数和返回值都只能是一个,那么在C-里,一个多参数多返回值的函数的定义和使用过程就是这样的:
struct Para...;//define a parameter struct struct Ret...;//define a return struct Ret func(Para p){ ... } Para para = XXX; Ret ret; ret = func(para);
result, err := func() result, _ := func()
c++大概是继承了c,所以只有一个i返回值。java从语法上模仿c++,所以也只有一个返回值。
设计一个多个返回值的语言,并不困难。golang就是一个。 个人理解,多返回值的函数一定可以通过多个单返回值的函数来实现。对于早起的语言设计者而言,这应该是一个锦上添花而不是必须的功能,优先级不高。
当然,后来的语言,很多已经实现了这个功能,因为毕竟不难
ps,说通过指针在参数里传递的,可以退散了,不是一个问题好吧。 现代编程语言中「函数」概念多来自 ALGOL 和 Fortran
ALGOL 和 Fotrean 的概念借自数学
数学函数有多元之说,然而你见过几个「多返回值」的? 这是一个好问题。为什么函数只返回一个值。这是存储和计算的一种统一。一个没有副作用的函数,返回一个值,那他在使用上其实是可以看做一个变量,一个有条件的变量,可以很好的简化程序。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...
