C language learning [4]-functions and scope rules

1. Function

A function is a set of statements that perform a task together. Every C program has at least one function, the main function main(). All simple programs can define other additional functions.

The function declaration tells the compiler the name, return type, and parameters of the function. The function definition provides the actual body of the function.

The C standard library provides a large number of built-in functions that programs can call. For example, the function strcat() is used to concatenate two strings, and the function memcpy() is used to copy memory to another location.

There are many names for functions, such as methods, subroutines, or programs, and so on.

The general form of function definition in C language is as follows

return_type function_name( parameter list )
{
    
    
   body of the function
}

In C language, a function consists of a function header and a function body.

All the components of a function are listed below:

1. Return type: A function can return a value. return_type is the data type of the value returned by the function. Some functions perform required operations without returning a value. In this case, return_type is the keyword void.
2. Function name: This is the actual name of the function. The function name and the parameter list together form the function signature.
3. Parameters: Parameters are like placeholders. When the function is called, you pass a value to the parameter. This value is called the actual parameter. The parameter list includes the type, order, and number of function parameters. Parameters are optional, that is, the function may not contain parameters.
4. Function body: The function body contains a set of statements that define the function to perform tasks.

2. Scope rules

In any kind of programming, the scope is the area where the variables defined in the program exist, beyond which the variables cannot be accessed. There are three places in the C language to declare variables:

  1. Local variables
    inside a function or block Variables declared inside a function or block are called local variables. They can only be used by the function or statements inside the code block. Local variables are unknowable outside the function.
  2. Global variables outside all functions
    are defined outside the function, usually at the top of the program. Global variables are valid throughout the entire program life cycle, and global variables can be accessed within any function.
    Global variables can be accessed by any function. In other words, global variables are available in the entire program after they are declared.
  3. In the function parameter definition of
    formal parameters, the parameters and formal parameters of the function are treated as local variables in the function. If they have the same name as the global variables, they will be used first.

In the program, the name of the local variable and the global variable can be the same, but in the function, if the two names are the same, the local variable value will be used, and the global variable will not be used.

全局变量与局部变量在内存中的区别:
全局变量保存在内存的全局存储区中,占用静态的存储单元;
局部变量保存在栈中,只有在所在函数被调用时才动态地为变量分配存储单元。

When a local variable is defined, the system will not initialize it, you must initialize it yourself. When defining global variables, the system will automatically initialize them, as shown below:
Insert picture description here

It is a good programming practice to initialize variables correctly, otherwise the program may produce unexpected results because uninitialized variables will cause some garbage values ​​that are already available in the memory location.

Guess you like

Origin blog.csdn.net/qq_46009608/article/details/110312658