当前位置:网站首页>What are functions in C language? What is the difference between functions in programming and functions in mathematics? Understanding functions in programming languages
What are functions in C language? What is the difference between functions in programming and functions in mathematics? Understanding functions in programming languages
2022-06-28 05:13:00 【Mengmengmuxi】
When many kids just learned programming , I don't know the difference between functions and methods .java Medium call method ,C Language is also called function ,javascript Also called function ... confused and disoriented .
Actually , They come from two words :function( function ) And method( Method ). The effect is basically the same , however , My personal understanding is : Methods are more class dependent , Exists in object / Classes are called methods , The function is not . And then gradually it was not a special distinction .
Just learning programming Language When , Some little friends don't know the function in the programming language . today , I will explain the role of functions in programming languages from the perspective of mathematical functions !
Well known , Functions in mathematics are generally like this :
y = 2 * x
It is estimated that this formula can be understood after having studied the most basic primary function estimation in Grade 8 or above .
that , When he was in x When taking different values ,y It can also change accordingly .
If you write this formula in your code , It looks like a general 2*x The result of is assigned to y ,y What you receive is 2*x Of expression Return value !
Pay attention to the word : Return value . In the future you will hear the word again and again .
At this point, if you write a separate calculation when x yes 2、3、8、10 Value ,
In Mathematics , Insert the above values one by one x Operation in , Get the corresponding y
The same is true in programming languages ,
however , Suppose you want to do some more complicated operations in the same way ( Here is the output sentence : Plug in xxx Carry out operations )
( For the convenience of demonstration , Open and write ):
#include <stdio.h>
int main(){
int x,y;
x = 2;
printf(" Plug in 2 Carry out operations \n");
y = 2*x;
printf("%d\n",y);
x = 3;
printf(" Plug in 3 Carry out operations \n");
y = 2*x;
printf("%d\n",y);
x = 8;
printf(" Plug in 8 Carry out operations \n");
y = 2*x;
printf("%d\n",y);
x = 10;
printf(" Plug in 10 Carry out operations \n");
y = 2*x;
printf("%d\n",y);
return 0;
}Because the operation is too simple , But if it's a very complicated equation , This makes the code redundant . Then we can use the concept of function .
The first C Let's talk about the format of language functions !
[ return type ] [ Function name ] ([ Function parameter ]...){
// The operation performed with the passed parameters
}
The return value type can be the basic data type ( Such as int,char,float,double...), The returned data is the data of the processing function !
that , The formula can be encapsulated into a function ( Suppose the function name is a):
int a(int x){
printf(" Substituted %d Calculate !\n",x);
return 2 * x;
}This is the most basic one C Format of language function , In the middle of the parentheses is the parameter . What values do you want to process , Just pass in the value ( You can also choose not to transmit anything , But parentheses cannot be omitted ), The type passed in must be the same as the type defining the parameter , The type of the received return value must be the same as the return value type !( For example, the return value of this example is int type , Also used for receiving int type )
here , The original code can be written as , You can also complete the same operation as just now :
# include <stdio.h>
int main(){
int x,y;
x = 2;
y = a(x);
printf("%d\n",y);
x = 3;
y = a(x);
printf("%d\n",y);
x = 8;
y = a(x);
printf("%d\n",y);
x = 10;
y = a(x);
printf("%d\n",y);
return 0;
}Some students will feel , This also returns a value , And then output . Since functions can be reused , Why not pack it all in ? You can do the same , But at this time, we should pay attention to : When a function does not need to return anything to the caller , The return value type can be written as void 了 , But this time , You can no longer take any variables to receive the return value of this function .
The complete code is as follows :
#include <stdio.h>
void a(int x){
// Declare a variable to save x The result of the treatment
int y;
printf(" Substituted %d Calculate !\n",x);
y = 2 * x;
printf("%d\n",y);
}
int main(){
int x;
// When calling a function, the function will output
// So there is no need to write one by one printf
x = 2;
a(x);
x = 3;
a(x);
x = 8;
a(x);
x = 10;
a(x);
return 0;
}here , Programming “ function ” The concept and mathematics of “ function ” The concept of . But it is obvious that functions in programming are much more flexible than those in mathematics !
边栏推荐
- Pcr/qpcr research: lumiprobe dsgreen is used for real-time PCR
- Binary sort tree: BST
- Based on the order flow tool, what can we see?
- Differences between pragma and ifndef
- IP datagram sending and forwarding process
- Voltage mode and current mode control of switching power supply
- 改性三磷酸盐研究:Lumiprobe氨基-11-ddUTP
- 现代交换原理MOOC部分题目整理
- BioVendor sRAGE Elisa试剂盒化学性质和技术研究
- Feign remote call fallback callback failed, no effect
猜你喜欢

Latest Windows version 5.0.14 of redis

Pcr/qpcr research: lumiprobe dsgreen is used for real-time PCR

Function reentry caused by Keil C51's data overlaying mechanism

Biovendor sRAGE protein solution

分享一个因子挖掘的利器:遗传规划

Generate QR code in wechat applet

改性三磷酸盐研究:Lumiprobe氨基-11-ddUTP

Dart学习——函数、类

基于订单流工具,我们能看到什么?

Lumiprobe细胞成像分析:PKH26 细胞膜标记试剂盒
随机推荐
Hundreds of lines of code to implement a script interpreter
Operation of simulated examination platform of G3 boiler water treatment recurrent training question bank in 2022
[microservices openfeign] openfeign quick start service invocation based on feign
The short video local life section has become popular. How to grasp the new opportunities?
Simple usage of GSAP
学习太极创客 — MQTT 第二章(六)MQTT 遗嘱
Have you finished the examination of level II cost engineer? There are also qualification regulations!
Blocking, non blocking, IO multiplexing select\poll\epoll
Performance degradation during dpdk source code testing
【LeetCode】12、整数转罗马数字
交流电和直流电的区别是什么?
RxSwift --(1)创建一个项目
Organize the online cake mall project
开关电源电压型与电流型控制
SlicePlane的Heading角度与Math.atan2(y,x)的对应转换关系
几百行代码实现一个脚本解释器
Differences between pragma and ifndef
Binary sort tree: BST
基于订单流工具,我们能看到什么?
8VC Venture Cup 2017 - Elimination Round D. PolandBall and Polygon