Wei iOS Basic Learning notes four C language basics-04 functions

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="color: #0000ff; font-family: 微软雅黑;"><strong>1. Arrays as Function parameters</strong></span></p></p><p><p><span style="font-family: 微软雅黑;">function is the most basic thing we learn the programming language, I will not repeat it here. Only a special case is discussed, that is, the array is passed as a function parameter.</span></p></p><p><p><span style="font-family: 微软雅黑;">As we all know, when we pass an array, we actually pass the pointer to the first element of the Array. With this in mind, we can think about the following questions.</span></p></p><p><p><span style="font-family: 微软雅黑;">Since he just passed the pointer to the first element of the array, he must not know the size of the entire array, so if we want to use the length of the array in the function, we have to pass it explicitly.</span></p></p><pre class="code"><pre class="code"><span style="color: blue;">int Sum (<span style="color: blue;">int a[],<span style="color: blue;">int Size)<br>{<br> <span style="color: blue;">int i, sum=0;<br> <span style="color: blue;">For (i=0;i<size;i++)<br> {<br> sum+=a[i];<br> }<br> <span style="color: blue;">Return sum;<br>}</span></span></span></span></span></span></pre></pre><p><p><span style="font-family: 微软雅黑;">So since the function cannot detect the length of the incoming array, we can also use this attribute to calculate the number of the first n of the array, or to use this feature to tell the function, in fact, the effective length of the array is less than the actual length of the Array.</span></p></p><p><p><span style="color: #0000ff; font-family: 微软雅黑;"><strong>2. Variable-length array in C99 as function parameter</strong></span></p></p><p><p><span style="font-family: 微软雅黑;">first, in the array section, we talked about the variable-length array in C99 as a good thing. So let's look at the case of variable-length arrays as function Parameters.</span></p></p><p><p><span style="font-family: 微软雅黑;">We look at the previous code, size and a[] and there is no direct connection, then the variable length array as a parameter will solve this Situation.</span></p></p><pre class="code"><pre class="code"><span style="color: blue;">int Sum (<span style="color: blue;">int size,<span style="color: blue;">int A[size])<br>{<br> <span style="color: blue;">int i, sum=0;<br> <span style="color: blue;">For (i=0;i<size;i++)<br> {<br> sum+=a[i];<br> }<br> <span style="color: blue;">Return sum;<br>}</span></span></span></span></span></span></pre></pre><p><p><span style="font-family: 微软雅黑;">This code explicitly indicates that the length of array A is size, which means that a direct connection is established between size and a[].</span></p></p><p><p><span style="font-family: 微软雅黑;">But here we need to be aware that the order of the parameters, the length must be written before the array, otherwise it will appear a[size] size error cannot be found.</span></p></p><p><p><span style="font-family: 微软雅黑;">There are several ways that we can do this when we are making a function declaration:</span></p></p><pre class="code"><pre class="code"><span style="color: blue;">int Sum (<span style="color: blue;">int,<span style="color: blue;">int a[*]);<br><span style="color: blue;">int Sum (<span style="color: blue;">int n,<span style="color: blue;">int a[n]);<br><span style="color: blue;">int Sum (<span style="color: blue;"><span style="color: blue;">int a[*]);<br><span style="color: blue;">int Sum (<span style="color: blue;">int,<span style="color: blue;">int a[]);<br><span style="color: blue;">int Sum (<span style="color: blue;">int n,<span style="color: blue;">int a[]);</span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></pre></pre><p><p><span style="font-family: 微软雅黑;">I recommend the first one, because I think the first is the easiest, and it can be shown that a is a variable-length array. Like the fourth and fifth, I personally think there are two very bad ways.</span></p></p><p><p><span style="font-family: 微软雅黑;"><span style="color: #0000ff;"><strong>3. The array parameter declaration in C99 uses the static</strong></span></span></p></p><p><p><span style="font-family: 微软雅黑;">The use of the keyword static in the array parameter declaration is allowed in C99. For example:</span></p></p><pre class="code"><pre class="code"><span style="color: blue;">int Sum (<span style="color: blue;">int a[<span style="color: blue;">static ten],<span style="color: blue;">int N)<br>{<br> <br>}</span></span></span></span></pre></pre><p><p><span style="font-family: 微软雅黑;">In terms of the function itself, static does not have any effect on the Function's own Implementation. Static 10 means that the length of the array is at least 10. then, when the function is called, the compiler will take 10 numbers out of memory beforehand, rather than taking them over and over again when the function is called, thus making the function more Efficient.</span></p></p><p><p><span style="color: #0000ff; font-family: 微软雅黑;"><strong>4. Return value of main function</strong></span></p></p><p><p><span style="font-family: 微软雅黑;">In the beginning of the C language, tan old books are mostly the same code:</span></p></p><pre class="code"><pre class="code"><span style="color: blue;">void Main ()<br>{<br> printf (<span style="color: #a31515;">"Hello world");<br>}</span></span></pre></pre><p><p><span style="font-family: 微软雅黑;">But in fact, this function has two flaws:</span></p></p><p><p><span style="font-family: 微软雅黑;">A. From A programmatic standpoint, it's best to explicitly declare that the main function has no arguments</span></p></p><p><p><span style="font-family: 微软雅黑;">B. The main function should return a status code, and in some operating systems, a status code can be detected at the end of the program to monitor whether the program ends PROPERLY. Even if you don't need this status code, other people may need it.</span></p></p><p><p><span style="font-family: 微软雅黑;">therefore, This function is best implemented as Follows:</span></p></p><pre class="code"><pre class="code"><span style="color: blue;">int main (<span style="color: blue;">void)<br>{<br> printf (<span style="color: #a31515;">"Hello world");<br> <span style="color: blue;">Return 0;<br>}</span></span></span></span></pre></pre><p><p><span style="font-family: 微软雅黑;">Remember when we said exit (0), we said earlier that it is no different to write return 0 and exit (0) in the main function. So let's take a look at the difference between return and exit.</span></p></p><p><p><span style="font-family: 微软雅黑;">Exit belongs to the <stdlib.h> header file, as we said before, 0 is the success in the status code, then for more intuitive, the C standard library provides us with such a two macro Definition.</span></p></p><pre class="code"><pre class="code"><span style="color: blue;">int main (<span style="color: blue;">void)<br>{<br> printf (<span style="color: #a31515;">"Hello world");<br> <span style="color: green;">Success |<br> <span style="color: green;">Failed<br>}</span></span></span></span></span></pre></pre><p><p><span style="font-family: 微软雅黑;">Let's turn to the definition to Discover:</span></p></p><pre class="code"><pre class="code"><span style="color: green;">/* Definition of the argument values for the exit () function */<br><br><span style="color: blue;">#define EXIT_SUCCESS 0<br><span style="color: blue;">#define Exit_failure 1</span></span></span></pre></pre><p><p><span style="font-family: 微软雅黑;">These two macro definitions are in <stdlib.h>. But these two values are not fixed, but are defined by the Implementation.</span></p></p><p><p><span style="font-family: 微软雅黑;">In addition, one of the most typical differences between return and exit is that calling return in other functions does not cause the program to terminate, but no matter where the exit is called, it will cause the program to terminate, and we'll look at a Program.</span></p></p><pre class="code"><pre class="code"><span style="color: blue;">int main (<span style="color: blue;">void)<br>{<br> printf (<span style="color: #a31515;">"begin\n");<br> Breaktest ();<br> printf (<span style="color: #a31515;">"end\n");<br>}<br><br><span style="color: blue;">int Breaktest ()<br>{<br> <span style="color: blue;">Return 0;<br>}</span></span></span></span></span></span></pre></pre><p><p><span style="font-family: 微软雅黑;">This code should not produce any questions:</span></p></p><p><p></p></p><p><p><span style="font-family: 微软雅黑;">Next look at this code:</span></p></p><pre class="code"><pre class="code"><span style="color: blue;">int main (<span style="color: blue;">void)<br>{<br> printf (<span style="color: #a31515;">"begin\n");<br> Breaktest ();<br> printf (<span style="color: #a31515;">"end\n");<br>}<br><br><span style="color: blue;">int Breaktest ()<br>{<br> Exit (exit_success);<br>}</span></span></span></span></span></pre></pre><p><p></p></p><p><p><span style="font-family: 微软雅黑;">so, exit makes the entire program Terminated.</span></p></p><p><p>Wei iOS Basic Learning notes four C language basics-04 functions</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.