search
HomeCommon ProblemThree ways to pass parameters to a function

Three ways to pass parameters to a function

Three ways to pass function parameters

1. Pass by value. When passing, the formal parameters and actual parameters each occupy an independent space.

2. Address transfer is to transfer the storage address of the actual parameter to the formal parameter.

3. Passing by reference is an alias with a reference as the actual parameter, which is the same variable as the actual parameter.

Sample code

Pass by value

#include<stdio.h>
void swap1(int x, int y)//定义中的x,y变量是swap函数的两个形参
{
	int tmp;
	tmp = x;
	x = y;
	y = tmp;
	printf("x=%d,y=%d\n", x, y);
}
int main()
{
	int a = 2;
	int b = 3;
	swap1(a, b);//a,b变量为swap函数的实际参数
	printf("a=%d,b=%d", a, b);
	return 0;
}

Pass by address

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);/*调用了swap函数,同样也有隐含动作px=&a;py=&b;*/
	printf("a=%d,b=%d", a, b);
	return 0;
}

Pass by reference

#include<stdio.h>
void  swap3(int &x,int &y)
{
	int tmp = x;
	x = y;
	y = tmp;
	printf("x=%d,y=%d\n", x, y);
}
int main()
{
	int a = 2;
	int b = 3;
	swap3(a, b);//调用方式与传值一样
	printf("a=%d,b=%d", a, b);
	system("pause");
	return 0;
}

Recommended tutorial :《C# tutorial

The above is the detailed content of Three ways to pass parameters to a function. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools