function (1)

content

What is a function?

Function classification in C language:

Library Functions

custom function

function arguments

function call

call by value

call by address

Nested calls and chained access of functions

nested calls

chained access


What is a function?

We often use functions in mathematics, but what are functions in C language?

A function is a subroutine that you can call directly when you want to use it.

In computer science, a subroutine (English: Subroutine, procedure, function, routine, method,
subprogram, callable unit) is a portion of code in a large program consisting of one or more blocks of statements
. It is responsible for completing a specific task and is relatively independent from other code.

Function classification in C language:

1. Library functions

2. Custom function

Library Functions

What are library functions?

A series of similar functions are provided in the basic library of C language, which is convenient for programmers to develop software. Such as: printf function, scanf function, strlen function and so on.

Note : To use library functions, the header files corresponding to #include must be included.

Here I share some query websites and software, everyone can learn:

MSDN(Microsoft Developer Network)
www.cplusplus.com
http://en.cppreference.com (English version)
http://zh.cppreference.com (Chinese version)

custom function

The functions we designed ourselves are called custom functions, which are not in the C language library. They also have function names, return value types and function parameters.


Such as:

//写一个函数可以找出两个整数中的最大值。
int Max(int a, int b)
{
	return a > b ? a : b;
}

Another example:

//写一个函数可以交换两个整形变量的内容。
void Swap(int a, int b)
{
	int tmp = a;
	a = b;
	b = tmp;
}

function arguments

The parameters of the function can be: constants, variables, expressions, functions, etc.
Actual parameters (actual parameters):
The parameters passed to the function are called actual parameters.
Regardless of the type of quantities the actual parameters are, they must have definite values ​​in order to pass those values ​​to the formal
parameters when the function is called. 

Formal parameters (formal parameters):
The parameters received by the function are called formal parameters.

Formal parameters are called formal parameters because they are only instantiated (allocated memory units) when the function is called. Formal parameters are automatically destroyed when the function call is complete. So formal parameters are only valid within functions.
Here we analyze the actual and formal parameters of the function:

int fun(int x, int y)
{
	int z = x + y;
	return z;
}

int main()
{

	int a = 10;
	int b = 20;
	fun(a, b);
	return 0;
}


 

Here you can see that when the function is called, x and y have their own independent space.

So we can think that the formal parameter is actually a temporary copy of the actual parameter.


function call

call by value

The formal parameters and actual parameters of the function occupy different memory blocks respectively, and the modification of the formal parameters will not affect the actual parameters.

void Swap(int x, int y)
{
	int tmp = x;
	x = y;
	y = tmp;
}

int main()
{
	int a = 10;
	int b = 20;
	Swap(a, b);
	printf("%d %d\n", a, b);
	return 0;
}

 

 

call by address

Call by reference is a way to call a function by passing the memory address of a variable created outside the function to the function parameter.
This way of passing parameters allows the function to establish a real connection with the variables outside the function, that is, the variable inside the function can directly manipulate
the variables outside the function.

void Swap(int* x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}

int main()
{
	int a = 10;
	int b = 20;
	Swap(&a, &b);
	printf("a = %d,b = %d\n", a, b);
	return 0;
}


Take the common printf and scanf functions, which are typical call-by- value and call -by -reference .

printf function

Pass the value of the data to the printf function, and the printf call window prints the data type you want.

scanf function

Pass the address of the parameter, connect the input device, and modify the value of the variable. If you do not pass the address, but pass the value, the modification of the formal parameter will not affect the actual parameter.


Nested calls and chained access of functions

nested calls

#include <stdio.h>
void fun()
{
	printf("hehe\n");
}
void test()
{
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		fun();
	}
}
int main()
{
	test();
	return 0;
}

Functions can be called nested, but not defined nested.

int main()
{
	void test()
	{
		printf("hello world\n");
	}
	return 0;
}
//这样是不对的

chained access

As mentioned earlier, every function has a return value. Can the return value of one function be used as a parameter of another function?

#include <stdio.h>
#include <string.h>

int main()
{
	printf("%d\n", strlen("abcde"));
	return 0;
}

It is possible to write the return value of the strlen function as the parameter of printf.

Guess you like

Origin blog.csdn.net/qq_54880517/article/details/123508200