C language basic syntax - function

  1. What is a function

  2. Function syntax

  3. Function declaration

  4. Function call

  5. Forms of functions participate in actual parameters

  6, return and exit keywords

  7. Recursive function

 

  1. What is a function

  • A function is a series of statements grouped together and assigned a name

  • The term function comes from mathematics, but is not exactly equivalent to mathematics

– Functions don’t have to have parameters
– Functions don’t have to compute values

  • Functions are the building blocks of the C language

  2. Function syntax

  • Functions are divided into three steps in the use step: declaration, definition, and invocation.

  • Syntax format

return value type function name (parameter) {

  ...(function body)(return value);

}

  3. Function declaration

  • Normally, functions must be declared before use

  • Since the compiler goes from top to bottom, the code below calls the function above and does not need to declare it, but when the code above calls the function below, it must be declared

  • Declaration format, remove the function body part, keep the return value type function name (parameter); (function prototype)

  • If the return type is not int, it is better to use a function declaration (function prototype).

  • void f();

   In function declaration, parameters can be omitted. Omitting parameters means that any parameters can be accepted. If no parameters are required, use the void keyword. void f(void);

  4. Function call

  • Need to write out the function name followed by the actual parameter list

  • Actual arguments are used to provide information to the function

  • The actual parameter does not have to be a variable, any expression of the correct type will do

  5. Forms of functions participate in actual parameters

  • The formal parameters of a function are the parameters used in the definition

int fa (int x) {

  return x * x + 2 * x + 4;

}

  • The actual parameters of the function are the parameters that need to be passed in when calling the function

int main() {

  int result = fa (10); // x = 10;

  return 0;

}

  6, return and exit keywords

  • When calling a function, the execution result of the function is required, then the return value type is required to identify the type of the result, and the return keyword must be used inside the function body to identify the specific function result (return value).

  • If the function does not need the result after execution, define the function's return value type void (empty), and the return keyword is not needed.

  • exit(0) is a function that exits the entire program needs to include a stdlib.h

  • return can be used as a keyword for return value to terminate the continuation of the current function.

  7. Recursive function

  • recursive algorithm

– Recursive process, generally implemented through functions or sub-functions

– Recursive method: Inside a function or subfunction, directly or indirectly call your own algorithm.

  • How recursion works

  Recursion has the potential to cause infinite loops or complication of algorithms. (more runs). Therefore, when using recursion, you must pay attention to:

- must have exit condition
- must save after recursion, algorithm simplifies

  • recursive format

int f1(int n) {
  printf("f1(int n) %d\n",n);

  if (n==1) {

    return 1;

  } 

  return n*f1(n-1);

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324893365&siteId=291194637