Parameter transfer method between C language functions

Author : houxue   2023-04-21 15:48:55 Browse: 746
Category : Python

Abstract: How parameters are passed when calling C language functions In the main function, define a variable first, then input a value, an...

How parameters are passed when calling C language functions

In the main function, define a variable first, then input a value, and output it in the a() function. When the program runs a (num); In this step, the value of num is assigned to num_back, and during the program operation, the actual parameter value is passed to the formal parameter, which is the transfer of function parameters.

There are only two transfer methods for functions: value transfer, also known as one-way transfer, which can only pass the actual parameter value to the formal parameter. The final result of the formal parameter does not affect the actual parameter (the size of the formal parameter changes, but the size of the actual parameter remains unchanged). Address passing, passing the address of the actual parameter to the formal parameter through a pointer, and the size of the formal parameter can affect the actual parameter.

There are three types of function parameter passing: value passing uses value passing, which actually copies the content of the actual parameter into the formal parameter. The actual parameter and the formal parameter are stored in two different memory spaces.

In C language, the actual parameter passes the value to the formal parameter in the function, achieving top-down value transfer, and the function can be called.

1: Value transfer simply copies the value of a variable into the calling function.

2: By reference transfer, the address (i.e. pointer) of a variable is passed to the calling function, and the operation performed by the calling function on the address of the variable will change the value of the parameter.

C language function value transfer

C Language

  • The standard function header should be written as void p(int a[]) instead of void p(int a[5]). Even if written as void p(int a[5]), the compiler interprets it as void p(int a[]) instead of void p(int a[5]), and the meaning of a[] here is to define a as an int * pointer.

  • This example is to help you understand what function passing is all about. The function parameter passing in C has always been value passing, don't be fooled by pointers. Pointers are just a variable. Because you passed the pointer and used the same pointer. Function passing parameters is value passing. That is to copy a copy of the pt value into it.

  • Between multiple files, it is generally rare to pass a single parameter. The intersection is too large and not easy to transplant. It is best to be as independent as possible between each file. Data transmission is generally achieved through functional interfaces.

  • The value of the actual parameter is passed to the formal parameter, which can be seen as a local variable in the called function. The called function can have a return value or no return value and can be returned with a return.

What are the characteristics of parameter passing in C language

C language is a procedural language with structured programming, variable scope and recursive function. C language passes parameters by value, and pointers can also be passed. Different variable types can be combined together using structures. Only 32 reserved word make variable and function naming more flexible.

The characteristics of C language: concise, compact, flexible, and convenient. C language only has 32 keywords and 9 control statements, and the program is written freely, mainly represented by lowercase letters. It combines the basic structure and sentence of high-level programming language with the practicality of low-level language.

The array name, as a parameter, will degenerate into a pointer during parameter passing, which means that the array name, as a parameter, passes the first address of the array.

The parameters of a function are divided into formal and actual parameters. Formal parameters appear in the function definition and can be used throughout the entire function body, but cannot be used without the function. The argument appears in the calling function, and after entering the called function, the argument variable cannot be used.

Incoming parameters are generally passed by value, meaning that modifications in the function are not reflected in the calling function. Outgoing parameters are generally passed by address, and modifications in the called function will be reflected in the main function.

    Sign in for comments!
Comment list (0)

Powered by TorCMS (https://github.com/bukun/TorCMS).