Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the ways of passing parameters to a function

Shulou Source: shulou.com Published: 2022-06-03 01:53:53 09月19日 Update

This article uses easy-to-understand examples to introduce what are the ways of function parameters, the code is very detailed, interested friends can refer to, I hope it can be helpful to you.

The main results are as follows: 1. The formal parameter and the actual parameter each occupy an independent space when passing by value.

2. Address transmission is to send the storage address of the actual parameter to the parameter.

3. Reference passing is an alias with reference as argument, which is the same variable as argument.

Sample code

Pass by value

# includevoid swap1 (int x, int y) / / the xpeny variable in the definition is the two formal parameters of the swap function {int tmp; tmp = x; x = y; y = tmp; printf ("x% d direction y% d\ n", x Magi y);} int main () {int a = 2; int b = 3; swap1 (a, b) / / the return b variable is the actual parameter printf of the swap function ("a moment% d direction banner% d", a moment b); return 0;}

Address delivery

Void swap2 (int * px, int * py) {int tmp; tmp = * px; * px= * py; * py= tmp; printf ("px=%d,py=%d\ n", * px, * py);} int main () {int a = 2; int b = 3; swap2 (& a, & b) / * the swap function is called, and there is also an implied action px=&a;py=&b;*/ printf ("averse% ddirection baked% d", a, b); return 0;}

Reference transfer

# includevoid swap3 (int & x int int & y) {int tmp = x; x = y; y = tmp; printf ("x% d direction yearly% d\ n", x, y);} int main () {int a = 2; int b = 3; swap3 (a, b); / / the call method is the same as the value passed printf ("aversion% ddirection b% d", a, b) System ("pause"); return 0;} about the way the function parameters are shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Function mode variable address parameter code more and reality help yes two examples interest content aliases actions parameters actual guys buddies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux MySQL Docker Huawei