Home > Backend Development > Golang > Do formal parameters in go language occupy memory?

Do formal parameters in go language occupy memory?

青灯夜游
Release: 2022-12-28 17:19:37
Original
4745 people have browsed it

Formal parameter variables do not occupy memory when there is no function call, they only occupy it when the function is called, and the memory will be released after the call is completed. The full name of the formal parameter is "formal parameter", which is the parameter used when the function is defined; however, the parameter does not have any actual data when the function is defined, so no memory is allocated for the formal parameter before the function is called. Its function is to indicate the type of the independent variable. and form and role in the process.

Do formal parameters in go language occupy memory?

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Function is an organized, reusable code segment used to implement a single or related function, which can improve the modularity of the application and the reuse rate of the code.

Function definition and function call

Functions constitute the logical structure of code execution. In Go language, the basic components of functions are: Key Word func, function name, parameter list, return value, function body and return statement. Every program contains many functions, and functions are basic code blocks.

Definition syntax:

func 函数名(形参列表)(返回值类型列表) {
  函数体,通常会有return语句,返回值
}
Copy after login
  • Function name: The identifier of the function, used to find the function, and internally is an address pointing to the function code.

  • Formal parameter list: composed of variables and types

  • Return value type list: the type of function return value, multiple return values ​​need to be specified Multiple.

  • Function body: the specific statement that implements the function. return statement: return value statement

Function call syntax:

函数名(实参列表)
Copy after login

Function parameters

is used to pass data to the function when calling the function.

  • Actual parameters, actual parameters. Parameters given when calling. Refers to parameters with specific actual data.

  • Formal parameters, formal parameters. Parameters used when defining. It means that the function requires parameters, but the parameters do not have any actual data when they are defined. Functions/methods do not allocate memory for them before being called. Their function is to describe the type and shape of the independent variables and their role in the process.

    The relationship between actual and formal parameters: formal parameters can only be variables (its data type must be specified); actual parameters can be variables, constants or expressions.

    The number and position of actual and formal parameters and their corresponding data types should be consistent.

#When called, the process of assigning values ​​to formal parameter variables using actual parameters occurs, which is called parameter transfer. During the execution of the function, the formal parameters have specific data, and the formal parameters are equivalent to the variables declared within the function. The passing of parameters is divided into two methods: value passing and address passing. When the address is passed, the formal parameters need to be defined as pointer types, and the address parameters need to be obtained when calling.

  • If the actual parameter is an array name when calling a function, the first address of the array will be passed to the formal parameter.

  • Passing actual parameters to formal parameters is one-way transfer. Formal parameter variables do not occupy memory when there is no function call, they only occupy it when the function is called. The memory will be released after the call is completed.

Sample code:

func funcTest(p1 int, p2 *int) {
  p1++
  *p2++
  fmt.Println(p1, *p2)
}
func main() {
  var (
    a1 = 42
    a2 = 42
  )
  funcTest(a1, &a2)
  // 参数赋值过程
  fmt.Println(a1, a2)
}
Copy after login

The above will output

43 43
42 43
Copy after login

[Related recommendations: Go video tutorial, Programming teaching

The above is the detailed content of Do formal parameters in go language occupy memory?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template