当前位置:网站首页>C language - Growth Diary -02- function
C language - Growth Diary -02- function
2022-06-11 07:42:00 【Zhangxueheng】
![]()

In the beginning c When it comes to language , It is quite possible that the sum , The code will be the same as shown above . We can write a code segment for summation , Then write the following code by copying and pasting .
Oneortwo lines of code , but , If it is tens of thousands of lines of code , Hundreds of summations , Then this method of writing step by step may not be so easy to use . And this form of expression has a proper noun :code duplication Code copy .
Code duplication is a sign of poor program performance . Because the copied code segment , Errors may occur in the future . Then it is not as simple as changing oneortwo lines of code , Instead, modify the copied code .
Is there a way , We can make the code more efficient ?
This is the topic I want to write today , function .

Let's bring out the repeated part , Written in the form of a function .
Suppose this function is called sum
sum( , ) Before and after the comma can be recorded as two variables , When you declare a function, you also write ,void sum(int begin,int end)
These two places are the places where the user enters the value he entered in the main function .
sum The function says
int i; ( It can be understood as an intermediate variable i )
int sum=0; // initialization sum
for loop (i=begin;i<=end;i++)
{
sum+=i;
}
You mean for :i from begin Start , To end end ,sum=sum+i namely : Sum up .
Then output printf......
It can be seen clearly , Although it's a little long to write functions , But it is very convenient to use !

Calling in main function , Just write the function name and the number of the required sum .
Very convenient ! And the interface looks very simple !
Even if there is a need for modification in the future , We only need to modify the function body itself , Instead of modifying a lot of lines of code .
The existence of function , It greatly simplifies the complexity of writing programs , And it improves efficiency , It looks very comfortable !
Think hard when you see problems , Xiao Zhang grew up with you !
c Language novices are just beginning to blog , There are many places that are not in place csdn My friends give me more advice !
Thank you very much. !
边栏推荐
- C language Yanghui triangle code
- [atcoder1984] wide swap
- Remote office experience sharing | community essay solicitation
- 二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...
- [atcoder2305] declining (game)
- 【AtCoder2304】Cleaning
- 测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
- Zero foundation self-study SQL course | outer join external connection
- Miscellany C language
- 【软件测试】这样的简历已经刷掉了90%的面试者
猜你喜欢

如何准备PMP新版大纲考试?

Simple configuration of vscade

二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...

Zero foundation self-study SQL course | union joint query
![[Oracle database] mammy tutorial day02 use of database management tool sqlplus](/img/f2/8f6f74a62427ebfb4c805c1e9b3352.png)
[Oracle database] mammy tutorial day02 use of database management tool sqlplus

JVM tuning

wordcloud的使用

【CodeForces1019E】Raining season(边分治+斜率优化)

C wechat upload form data

Seata的几种事务模式
随机推荐
Post-processing of ffmpeg miscellaneous notes
2022 low voltage electrician test questions and online simulation test
【AtCoder2305】Decrementing(博弈)
You got 8K in the 3-year function test, but you were actually pretending to work hard
【IoT】项目管理:如何打造更好的跨职能团队?
[atcoder2000] leftmost ball (dp+ combination number)
Paging of the flask page
[atcoder1981] short diameter (graph theory thinking)
. Net C Foundation (6): namespace - scope with name
20200810 T2 dispatch money
【AtCoder2000】Leftmost Ball (DP+组合数)
Simple use of string
QT table display data
Zero foundation self-study SQL course | outer join external connection
【集群】LVS+keepalived高可用集群
Ffmpe a small demo to understand 80% of common APIs
Classes and objects (medium)
Aiop introduction
【集群】haproxy负载均衡
Qunhui ds918 creates m.2 SSD read / write cache