Table of Contents
回复内容:
Home Backend Development PHP Tutorial javascript - 请问怎么理解计算机中的函数?

javascript - 请问怎么理解计算机中的函数?

Jun 06, 2016 pm 08:07 PM
javascript php function computer computer Science

之前的个人理解觉得不妥已删

类似于这样的说法就不要回答了:函数是一个工厂,你放原料进去,它生成产品出来。

回复内容:

之前的个人理解觉得不妥已删

类似于这样的说法就不要回答了:函数是一个工厂,你放原料进去,它生成产品出来。

你的理解有点外行看热闹的意思,呵呵。
代码本身就是抽象的,所以“计算机中的函数是一种对代码进行抽象的方式”不能说不对,但是也和没说一样。至于“我们使用抽象出来的函数,而不用关心函数里面的代码是如何组织的”,只能说是一种诉求,实际上很多错误正是在函数内部产生的,因此我们不可能完全不关心函数的内部实现。

从语义上来说,函数是行为的具体实现,行为通常是动词或动词短语,如get()[获取], getDetails()[获取详情], getDetailsById()[根据id获取详情]。
从开发的角度来说,函数是一种代码结构,如果一段代码会被使用2次及以上,那么将这段代码放在函数中可以使其更易于维护。
从可读性上说,函数相当于名词解释,用简短的名字表达一个相对复杂的事情,这样阅读起来比较愉快,也不容易歪楼。例如 读取用户详细数据并显示 明显好于 连接数据库,在用户表执行查询获得指定id的用户返回的句柄,获取句柄游标指向的行并转换为数组,然后显示该数组。后者不但很长,明显不是人话,增加沟通成本。

总结:无论是对象还是函数,都是一种代码的组织形式,其目的是提高代码的可读性、使其易于维护。不同的人由于思维习惯的不同,其代码的组织形式可能不同,但是通常都是自洽的。(白话的说法,有人喜欢3句话描绘一件事情,具体实现时每句话又会拆分为3句话的描述,有人喜欢5句话描绘一件事情,具体实现时每句话又会拆分为5句话的描述,那么这两个人定义的对象/函数必然不同,但最后会得到相同的结果。)

常见的书本上的函数

<code>f(x)=x+1;</code>
Copy after login

更改到代码

<code>function f(x)
{
    return x+1;
}</code>
Copy after login

如果还不理解,那初中就白上了

1.函数是一种映射关系,我们的输入对应一种输出(一般不强制限定为一种输出),输入可以为0。
2.函数是对可复用代码的封装。我么写的一段代码块,根据它的可复用性,去选择封装代码还是不封装代码,封装好了,就可以称之为函数。
3.函数是对编程的IPO模式的细节的隐藏。我们进行编程,肯定会遵循IPO模式,即输入-处理-输出。我们隐藏其中的处理过程时,只需要测试好输入和输出,就可以说封装了一个函数。
4.函数内部是组织好了的代码块。
总而言之,函数是一种封装了可复用代码的具有I/O映射关系的代码组织形式

谢邀。参考 wikipedia

In different programming languages, a subroutine may be called a procedure, a function, a routine, a method, or a subprogram. In computer programming, a subroutine is a sequence of program instructions that perform a specific task, packaged as a unit.

函数是子例程的别名。子例程是一段执行特定任务的程序操作,以封装好的单元形式存在。

通俗地说,函数是一段小程序,指定一定的参数,执行一段对参数或外部环境有影响的操作,并可选地返回一个值。

好处?自然是 D.R.Y(DONT REPEAT YOURSELF)了。它简化了相似性代码的书写,同时也使程序模块化,易于维护。

(这里的函数和数学里的函数还是不一样的好伐→_→→_→→_→→_→)

函数就是实现某种功能的代码片段的封装,比如: sum()函数,就是求和,求和就是功能,用
关键词 function 包起来的代码块或者片段,代码函数具体的代码,你不用知道,因为作者已经
给你封装好了,你只需要调用即可,sum(1,2) 的结果是3,你立即知道了结果,但你不用知道函数内部
的如何计算的

<code>function sum($a=0,$b=0)
{
    return $a+$b;
}</code>
Copy after login

初中数学里的函数,你理解不?

一样一样的。

我觉得只要理解函数是什么作用即可,而不用过深的去了解函数这个概念本身!

因为程序语言中的“函数”本身也是一种封装,需要解释器统一对函数语法进行解析!

和你自己封装了一个函数给N个程序复用是一样的,没有太多的本质区别。

如果还不知道作用,你学习一门新的程序语言要打印hello world的时候,你发现程序语言没有给你封装一个叫做打印的函数,我不知道你当时是一种什么编程体验?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1251
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles