C language foundation - function (on)

Table of contents

1. What is a function

2. Classification of functions

1. Library functions

2. Custom functions

3. Function parameters

1. Formal parameter definition

2. Definition of actual parameters

3. The relationship between actual participation and formal parameters 

 4. Function call

1. Call by value

2. Call by address

3. How to choose

5. Summary


1. What is a function

Speaking of functions, the first thing we think of is definitely the mathematical functions, and these function problems can be said to torture us to death in mathematics. But did you know that there is also a function in C language , which has many similarities with mathematical functions, and it is also the knowledge that must be mastered in learning C language .

function definition

A function in C language is a piece of code that can be reused to independently complete a certain function . It can receive parameters passed by the user, or not ; the process of encapsulating a code section into a function is called function definition . If there is a return value , use the return statement in the function body to return . The type of the returned data should be the same as the data type of the received value .

2. Classification of functions

  • Library Functions
  • custom function

1. Library functions

What is a library function?

The C language standard states:

  • function of function
  • Function name
  • parameter
  • return type 

This series of functions is then made by the compiler manufacturer according to these regulations and placed in the standard library, so it is called a library function .

The functions that come with the compiler,  such as our commonly used scanf, printf, strlen, etc., are all library functions.

Here is a website for friends to let you know all the library functions in C language: https://cplusplus.com/reference/

Here is a brief summary. The library functions commonly used in C language are:

  • I/O function
  • String manipulation functions
  • character manipulation functions
  • memory manipulation function
  • time/date functions
  • Numeric function
  • Other library functions

 Some library functions with similar functions will be put into the same header file, so if you want to use these library functions, you must first call their header files , such as our commonly used <stdio.h>, <string.h>, <time.h> and so on.

2. Custom functions

Although library functions are easy to use, they cannot meet all our programming needs . At this time, programmers are required to define functions .

Custom function composition:

ret_type fun_name(para1,*)

{

statement ;//statement item

}

ret_type return type

fun_name function name

para1 function parameter

Here we give an example to explain the function in detail:

 Write a function to implement the addition operation:

#include<stdio.h>
int Add(int x,int y)
{
    int sum = 0;
    sum = a + b;
    return sum;
}
int main()
{
    int a = 2;
    int b = 5;
    int sum = Add(a,b);
    printf("%d",sum);
    return 0;
}

As in the above function, int is the return type of this function , which means that the return value of this function is an integer. At this time, you need to use return to return your sum specifically .

One thing to note here is that when the return value of the function is not empty , a variable of the same type as the return value must be defined in the main function to receive it .

Add is the function name . When creating a function name, try to use the English name of the function realized by this function, so that it is easier for others to understand what your function does.

int x, int y , are the two parameters required by this function, used to receive the value passed in , and use the operation in the function statement.

Among them, the return type and parameters of the function are optional according to your actual needs , such as the following functions:

#include<stdio.h>

void print()
{
    printf("hello world!");
}
int main()
{
    fun();
    return 0;
}

The return type of a function can also be void , that is, it does not return any value .

It is also possible to implement a function of printing "hello world!" without parameters .

3. Function parameters

Function parameters are divided into two types:

  • formal parameters
  • actual parameters

1. Formal parameter definition

  • Formal parameters are variables enclosed in parentheses after the function name .
  • Therefore, formal parameters are only instantiated when the function is called , so they are called formal parameters.
  • The formal parameter is automatically destroyed after the function call is completed, so the formal parameter is only valid in the function .

2. Definition of actual parameters

  • The parameters actually passed to the function are called actual parameters.
  • Actual parameters can be: constants, variables, expressions, functions, etc.
  • No matter what type of quantity the actual parameters are, they must have definite values ​​when the function is called , so that these values ​​can be transferred to the formal parameters.

3. The relationship between actual participation and formal parameters 

Write a function to exchange two integers:

#include<stdio.h>
void Swap(int x, int y)
{
	int temp = x;
	x = y;
	y = temp;
}
int main()
{
	int a = 2;
	int b = 5;
	Swap(a, b);
	printf("%d %d", a, b);
}

Friends, guess, can this function really exchange the values ​​of a and b?

 The answer is no.

Why is this? ? ?

The x and y we defined in the above function definition are called formal parameters , while a and b passed into the function are called actual parameters .

Let's look at the picture above. Have you noticed that although we let x and y receive the values ​​of a and b respectively, their addresses are not the same .

Friends, don’t forget that main is also a function . The addresses of the variables created in different functions will definitely not be the same , so x and y only receive the values ​​of a and b, but not The addresses of a and b are received .

If the value of this variable is not changed through the address outside the function , the value of the variable itself will not be changed .

Therefore, the formal parameter is only a temporary copy of the actual parameter , and any modification to the formal parameter will not affect the actual parameter .

Then we can't realize the exchange through the function? ? ?

Of course, since you need an address to change a value in different functions , we will use the address to operate:

#include<stdio.h>
void Swap(int* x, int* y)
{
	int temp = *x;
	*x = *y;
	*y = temp;
}
int main()
{
	int a = 2;
	int b = 5;
	Swap(&a, &b);
	printf("a = %d b = %d", a, b);
}

For the above function, we know that the pointer is operated by directly receiving the address of the variable , so we define the formal parameter of the function as two pointer variables , and pass in two addresses of &a and &b to the function for it to receive, and then We can easily get it done by exchanging the values ​​of the two pointer variables.

 Let's look at the addresses of x and y at this time, are they the same as a and b?

 4. Function call

1. Call by value

  • Call by value, as the name implies, is to pass a value to a function.
  • Features: The formal parameters and actual parameters of the function occupy different memory blocks , and the modification of the formal parameters will not affect the actual parameters .

Examples are as follows: 

#include<stdio.h>
void Swap(int x, int y)
{
	int temp = x;
	x = y;
	y = temp;
}
int main()
{
	int a = 2;
	int b = 5;
	Swap(a, b);
	printf("%d %d", a, b);
}

2. Call by address

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

 Examples are as follows:

#include<stdio.h>
void Swap(int* x, int* y)
{
	int temp = *x;
	*x = *y;
	*y = temp;
}
int main()
{
	int a = 2;
	int b = 5;
	Swap(&a, &b);
	printf("a = %d b = %d", a, b);
}

3. How to choose

So how should we distinguish and choose whether to pass by value or by address? ? ?

It's very simple. If the operation of your function requires changes to the actual parameters, call by reference . If you don't need to change the actual parameters, use call by value . Learn to make the right choice in different application scenarios.

5. Summary

This issue of Function Knowledge (Part 1) is coming to an end. Let’s take a sneak peek at the content of Function Knowledge (Part 2):

  • Nested calls and chained access of functions
  • Function declaration and definition
  • function recursion

If you have any questions or knowledge additions to the blogger's article, please feel free to comment on the private message.

Friends who like the blogger's articles, don't forget to click three times , see you in the next issue!

Guess you like

Origin blog.csdn.net/2303_78442132/article/details/132257049