8 Functions

function

1. The function of the basics

Function in mathematics, everyone is familiar with, the general form of f (x) = y, x is the independent variable, y is the function value. Function in the program, and mathematics function basically the same, there are arguments, we called "pass parameters", as well as function values, we called the return value.

  • The first part, a function of the type of return value.
  • The second part is a function name, call the function when the need to give the function name, the function name in the design of the time, to try to design a function related to the function names, such as functions in the figure above, by the name of our shows that this is a function of the square of the requirements.
  • The third part is an incoming parameter is a mathematical function arguments.
  • The fourth part is the body of the function, i.e. to complete the function-logic code, the result is the value returned by the return statement, and the logic to wrap the entire body of the function inside the curly braces.
#include <stdio.h>
int square(int x) { // 定义函数 square
    return x * x;
}
int main() {
    int n;
    scanf("%d", &n);
    printf("%d\n", square(n));
    return 0;
}

In the above code, the main function, we read in an integer number n, and then outputs the square value of n. Here calculating a square value of n, the program calls a function of the square defined above, then the output of the printf function corresponding to the function is the return value of the square, the square function depending on the implementation, if the incoming value x, then returns value is x * x, that is, squared value of x.

2. The general function of the variable transmission parameters

Function parameters transfer process, the process is independent of each other, "arguments" for "parameter" assignment "argument" and "parameter", independently of each other.

#include <stdio.h>
void add(int n, int m) {
    n += m;
    return ;
}
int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    add(n, m);
    printf("%d\n", n);
    return 0;
}

Function parameters transfer process is the process of "arguments" for "parameter" assignment.

In this procedure, the main function of the variable n is "argument", the Add function of the parameter n is the "parameter", although both the same name, but is completely independent of each other two variables.

3. The transfer function of the array of reference

#include <stdio.h>
void add(int *p, int n) {
    for (int i = 1; i < n; i++) {
        p[0] += p[i];
    }
    return ;
}
int main() {
    int arr[10] = {1, 2, 3};
    add(arr, 3);
    printf("%d", arr[0]);
    return 0;
}

You run this program, you will find the resulting output is the value of the first element of 6, meaning that the array has changed. Think again today we have to remember that sentence: function parameters transfer process, the process is independent of each other "arguments" to "parameter" assignment, "argument" and "parameter", independently of each other .

That which we are, "argument" is actually the first address of the array, the parameter is a pointer variable that function parameters are stored in the first address. In other words, within the add function space that array address space operations, and is the main function of a space, which is one reason why an array, the value of the relevant elements within the function can be passed Why get rid of, because delivery is the address!

4. Incoming and outgoing parameters

#include <stdio.h>
void calc(int x, int *p) {
    *p = x * x;
    return ;
}
int main() {
    int n, m;
    scanf("%d", &n);
    calc(n, &m);
    printf("%d\n", m);
    return 0;
}

Calc start by defining a function, calc function has two parameters, the first parameter is an integer, the second address is an integer, the internal function, the square value of x is stored into the memory space pointed to by p . Call the main function of the calc function, respectively incoming address value of n and m, and the output value of m, and finally you will find that the output value of m is n squared.

First, the first parameter x, is passed outside a value, this value inside the function, to participate in the important process of operation, that is to say, this feels more like a value passed from the outside to the inside, then the internal function play a role in this type of argument, we'll call "parameter passed."

Look at the second parameter calc function is passed an address. After the action inside the function, just pointed at this address space, and the results recorded inside the function is of no use to calculate some of the results obtained in storage, is to wait until the function is completed, return to the caller , for example, inside the main function of the above, to be useful. This is the role of a parameter, the value is more like a band from the inside out calc, the main function of these parameters to the interior design, we called the "spread parameter."

As seen in the above code, "incoming parameter" General is to pass into the value on the line, and "came parameter" Because should bring out the value from a function, generally pass into the variable address, so internal functions can accurate the result is written to the relevant memory corresponding to the address.

Questions (1): Array think and function

Similarities and Differences Consider the following two concepts:
an integer array element, for example: arr [100]
an incoming integer function and returns an integer, for example: func (100)

第一个是申请的占用连续内存空间的数组元素
第二个是申请100个整型数据,内存空间不一定连续
数组只能一次返回一个,函数可以返回多个

Questions (2): knowledge of how to determine the existence of blind

What is "blind spots of knowledge" mean? When you face is dark, you can determine there must be knowledge, not just darkness. Like today we learned the knowledge function, naturally, it will ask ourselves the way, these are all a function of the knowledge of it? How do we determine the answer to this question it? Very simple, according to the known push unknown.

to sum up

  1. The action function is a function to make the package to be reused elsewhere in the program-related functions.
  2. Parameter passing process function in C language, is argument to a procedure parameter assignment, change the value of the parameter, it does not affect the argument.
  3. In the design of function parameters, we have to make clear, incoming and outgoing parameters in function of the difference parameters.

Guess you like

Origin www.cnblogs.com/liugangjiayou/p/12609415.html