Function Learning and usage

Source: Internet
Author: User

Talking about function applications



The meaning of a function:

Splits the complex logic in the main function and processes a separate module by a single function. The main function only plays a macro-control role.


We can write some functions by ourselves to implement some functions and put the function declaration in. in the H file, put the function definition in. M file, and then in main. the main function in the M file is called for implementation, but before calling it, do not forget to import the file you write into the header file, and import the header file you write with # import "", # import <>


In addition, a function can contain up to 60 lines of code, 20 to 40 lines of code, and a file can contain up to 300 lines of code.

 

2,Function Definition: contains four parts
1), return value type (data type to be returned)
2), function name (give the function a name for unique identification function) Naming Convention: contains multiple words, except the first word, other Single words are capitalized (engineering names are also composed of words, and each word should be capitalized)
3), parameter list (used to receive incoming data)
4), function body (Real Functions of the function, actual operations)

 

3. functions are used in three parts:
1), function declaration, (written in. h file)
2), Function Definition, (function implementation) (written in the. M file)
3), function call (implemented in the main function in the main. M file)

 

Compared with the function definition, the function declaration only lacks the function body and adds a semicolon at the end.

To make it clearer: a function is used to implement a separate function. Only Code related to the function can be written in the function.

 

4. There are four formats for Function Definition:


1), the first format of Function Definition: No parameter, no return value (void is used when no return value is returned)

Void like ()
{
Printf ("I like learning IOS \ n ");
}
2), the second format of Function Definition: There are parameters, no return value (void is used when no return value is returned)

3) The third format of the Function Definition: No parameter.
Int givemoney ()
{
Printf ("continue to cheer, \ n ");
Return 100;
}

Return is used to return the value. The returned data is placed after return. Whoever calls the function will return the value
Remember not to put the code behind return, and the code will not be executed

 

4) The fourth format of Function Definition: parameters and return values


5. Real parameters and Parameters


Real parameters are actual parameters, and the actual specific values are called real parameters.
The form parameter is a form parameter. It is used only to indicate the type of data to be received. The specific storage data is unknown, that is, the parameter specified during function definition is a form parameter.
In addition, it is important to note that when a function is called, the transfer of real parameters to form parameters is a copy process. Functions and functions can be called in a nested manner, but cannot be defined in a nested manner;


6. Scope of Variables

Variables defined within a function can only be valid within the function. They are not accessible outside the function, so they are called local variables. (when a function is called, it opens up space for variables, when the function execution is complete, the system recycles the space.

Global variables can be accessed in all functions. The space is not reclaimed during the program running.

The static variable is a static variable. It has the following features:
1), the space is only opened once, and the space is not recycled during the cloud Period
2) the variable is initialized only once.
3) If no initial value is assigned, the default value is 0;


This article from the "personal IOS growth history" blog, please be sure to keep this source http://9178463.blog.51cto.com/9168463/1529952

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.