OBJECT-C Foundation (4)--function

Source: Internet
Author: User

C is a structured programming language, and Objective-c is a superset of C.

Why do you define a function?

To facilitate reuse of a piece of code, this code can be defined as a function, and each time the function is called, it is equivalent to the program to execute the code.

Syntax for defining functions (function)

return value type function name (formal parameter list)

 {

 }

return value type: can be any valid type (base type, constructed type, reference type).

The so-called return value type, which determines the type of result returned after the function is successfully executed.

Therefore, if there is no return value type, the return value type should be declared void.

If the return value type is not written, the return value type is int.

If the return value type is declared (including not written), the program should use the return statement in the body of the function to return the function's return value.

function Name: identifier.

In fact, a function name should be a concatenating of one or more meaningful words.

formal parameter list: parameter type parameter name, parameter type parameter name, ...

Each parameter must consist of a formal parameter type and a formal parameter name.

Calling functions

Note the point:

1. When declaring a function, several parameters are specified, and the function is called with a number of arguments, and the type of the parameter is consistent.

2. The return value of the function, which can be used either as a variable or immediately (put the return value of a function into an expression).

function declaration

the C language is top-down, the default requirement: The function must first be defined and reused.

for the following two scenarios:

-the function is defined at the back of the function call.

-the function is defined in another source file.

You need to use a function declaration at this point--the function declaration tells the compiler that the function exists.

Function declaration: Is the return value type of the important function, function name, formal parameter type can, do not perform the function body.

Note: A function declaration can have both a physical parameter name and no formal parameter name.

import statement, which is a large number of function declarations.

since the function declaration must precede all function calls, the program needs to place the #import statement at the top of the program.

The function's parameter-passing mechanism

The object-c mechanism is "value passing", and the program passes in the function just a copy of the parameter itself (the replica),

As a result, changes to the parameters of the function have no effect on the parameters themselves.

Note: In the case of passing pointers, it is actually a copy of the pointer that is still being passed.

Recursive functions

If a function calls itself, the function forms a recursive function.

Recursive, an implicit loop can be implemented.

like loops, recursion must also have an end.

Recursion: Be sure to have an end when you want to recursively return to the known direction.

Use the array itself (the essence of the array is the pointer) as the parameter

Attention:

1. When declaring an array-type parameter in a function, you can specify either the length of the array or the length of the array, but are not usually specified.

2. Since the array itself is a pointer, the incoming function is not the array itself, but a copy of the pointer.

Therefore, the function cannot get the length of the incoming array through sizeof.

3. Since the array itself is a pointer, the modifications made to the function's array elements affect the elements of the arrays themselves.

intrinsic and external functions

The C language is a structured programming language, so the function is a Class C citizen:

A C program is usually due to a number of functions, programmer-developed functions, may also need to rely on the system built-in functions.

thus: C language programs, may consist of thousands of functions, but there can be at most one main function

--it acts as the entrance to the program, and other functions are directly or indirectly called by the main function, and all functions are equal in status.

Each function can call another function, or it can be called by another function.

One of the functions of the static modifier: To turn external things (functions, global variables) into inner things (functions, global variables).

-the static function, which is an intrinsic function, can only be called in the current source file.

-functions that are not static or extern-modified can be called anywhere (as long as the function declaration is done first).

For intrinsic functions, a program can invoke an intrinsic function only by import the source code directly into it.

While external functions, even if the program does not get the source code to define the function, as long as there is a library of source code that defines the function, the program can also invoke the external function after the function declaration.

Local variables and global variables

The variables of C are divided into:

Local variables: variables defined in a function, in a method, are local variables.

Local variables are stored in their respective stacks, and the stack area is destroyed at the end of the function and method execution.

Characteristics: Only in the function that defines the variable, the method is valid, the function, the method executes, the local variable disappears immediately.

Global variable: A variable defined in the scope of a file, which is a global variable.

Global variables are stored in the entire program's memory (static storage area). The program does not end, and the static store does not disappear.

Feature: Global variables will remain in effect as long as the program does not exit.

Subdivision of local variables can also be divided into:

-Formal parameters: Starting from the invocation of the function, the parameter begins to take effect, and when the function ends, the argument is invalidated.

-The local variable in the method: from the time the variable is defined, the variable is invalidated when the function ends.

-A local variable of a code block: The variable is valid from the time it is defined, and the code block ends when it is invalidated.

Variables that are defined in the initialization statement of the IF condition body, the loop body, the for loop, are local variables of the code block, and leave the curly brace area that defines the variable, and the variable is invalidated.

Subdivide Global variables

-The static global variable, which is the internal [global] variable-can only be called in the current source file .

-global variable with no static modifier, can be called anywhere (as long as the variable declaration is done first).

Variable declaration: A variable declaration must be made if the program is to use a global variable defined in another source program. The syntax is:

extern type variable name;

Storage mechanism for local variables:

When defining a local variable, there are several modifiers:

Auto-has auto and no auto effect is the same. That is, all of the local variables you define, by default, are equivalent to having an auto.

Auto-Modified local variables are saved to the stack of functions or methods.

Static-places the local variable in the static storage area.

for a static local variable, the static local variable will remain in effect as long as the program does not exit.

because the static local variable is always valid, the static local variable is assigned the initial value only if the function is called by the program for the first time.

Register-controls the placement of local variables into registers.

However, the OS does not guarantee that the register variable will enter the register, so there is really not much to this modification.

On the principle of defining variables:

1. The program should take precedence over local variables (global variables are very bad design, which can result in reaching effects).

2. If the local variable needs to be able to save the last run of data at the end of the function or method, or if the variable only wants to be initialized the first time it is called,

The local variable should be decorated with static.

3. There is really no way to consider using global variables.

Typically, when a program unit needs to share data with another program unit, the correct approach is to pass the parameter instead of the global variable.

The static modifier has 2 effects:

-Modify global functions, global variables, and make them internal functions, internal global variables .

-modifies the local variable to make it a static local variable--the static local variable is saved to the static store of the program.

The extern modifier also has 2 effects:

-declares a global variable.

-modifies the external function. ———— but actually, the same is true without the extern effect.


Pretreatment

Preprocessing: The program processes calls at compile time, and these preprocessing directives are not persisted to the runtime.

For the preprocessing contained in the source program, the compiler finishes processing at compile-time and there is no preprocessing instruction at runtime.

Pre-processing characteristics:

-usually at the beginning of the program.

-Must start with #.

#define OR #undef定义宏变量和取消宏

#define定义的又被称为: Macro variable.

#define的作用是: The compiler performs a find, replace, before processing the program.

There are two benefits to defining a macro variable through # define:

-can be easily modified after the program.

-can improve the readability of the program.

#undef: Canceling macro definitions

#define还可以定义带参数的宏

For macro definitions with parameters, there are two points to note:

-When you define a macro's parameters, it is not the parameter that defines the function, so the parameters of the macro do not need to specify the parameter type.

-When using the macro parameters, be sure to place the arguments in parentheses.

Use the #ifdef #ifndef #else #endif执行条件编译.

Its usage is basically the same as the branch described earlier.

Their primary role is to determine whether the specified macro exists and to perform the corresponding branch based on the presence of the specified macro.

Use # if #elif #else #endif to perform conditional compilation.

The pre-processing if branch is much more efficient than the normal if branch.

If you can use a preprocessed if branch, you should try using a preprocessed if branch.

Remember: As long as your if condition contains expressions such as variables, function calls, you can only use normal if branches.

#include和 #import

The functionality of the #include和 #import is similar, but #import is more powerful.

#import就可以自动避免重复include导致的错误.

OBJECT-C Foundation (4)--function

Related Article

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.