Calling functions in C language

Table of contents

Function call form

Function name

function call statement

function expression

Function parameters



 

Function call form


The forms of function calls are as follows:

  1. Call by value: This form passes the value of a variable to the formal parameter of the function.

  2. Call by reference: This form passes the address of the variable directly into the function.

  3. Call by address: This form is implemented through a pointer, passing the variable reference into the function.

Function name


The function name can be any valid identifier. It should be short and meaningful, clearly expressing the function's function. Function names usually use lowercase letters. If the function name contains multiple words, they can be separated by underscores. When calling a function, you need to use the function name followed by a pair of parentheses and put the parameter list inside the parentheses. For example, if you have a function called "add" that takes two integer arguments and returns their sum, you can call it like this: result = add(2, 3).

function call statement


The following is a simple C code example that demonstrates how to define and call a function:

 
 

c复制代码

#include <stdio.h>
// 函数定义
int add(int x, int y) {
return x + y;
}
int main() {
int a = 2;
int b = 3;
int result;
// 函数调用
result = add(a, b);
printf("The sum of %d and %d is %d\n", a, b, result);
return 0;
}

In the above code, we have defined a function called "add" which accepts two integer arguments and returns their sum. In the main function, we declare three integer variables a, b and result, and set the values ​​of a and b to 2 and 3 respectively. Then, we called the add function and passed a and b as parameters to it. The result returned by the function is stored in the result variable. Finally, we use the printf function to print out the values ​​of a, b, and result.

function expression


Function expression is an important concept in C language, which is used to define the function and parameters of the function. Function expression consists of function name, parameter list and function body.

The following is a simple C code example that demonstrates how to define and call a function:

 
 

c复制代码

#include <stdio.h>
// 函数定义
int add(int x, int y) {
return x + y;
}
int main() {
int a = 2;
int b = 3;
int result;
// 函数调用
result = add(a, b);
printf("The sum of %d and %d is %d\n", a, b, result);
return 0;
}

In the above code, we have defined a function called "add" which accepts two integer arguments and returns their sum. The function expression is: int add(int x, int y). Among them, intthe return type of the function is an integer type, addis the function name, xand yis the parameter list of the function. The function body is: return x + y;, which returns the sum of the two parameters. In the main function, we declare three integer variables a, b and result, and set the values ​​of a and b to 2 and 3 respectively. Then, we called the add function and passed a and b as parameters to it. The result returned by the function is stored in the result variable. Finally, we use the printf function to print out the values ​​of a, b, and result.

Through function expressions, we can decompose complex functions into simple functions, making the code easier to understand and maintain. Functions can also be reused to avoid writing the same code over and over again.

Function parameters


In C language, function parameters can appear in the following forms:

  1. Pass by Value: In this method, the function receives a copy of the actual parameter instead of the actual parameter itself. Therefore, inside the function, the copy and the original variable are two completely independent entities, and modifying the value of the copy will not affect the value of the original variable.

For example:

 
 

c复制代码

void add(int a, int b) {
a = a + b;
}

In this example, a and  b are value parameters. Inside the function  add , a and  b are  main copies of the variables in the function. So, even if  the value add is changed inside the function  a , the value main in the function  a will not change.
2. Pass by Reference: In this method, the function receives the address of the actual parameter instead of the value of the actual parameter. Therefore, inside the function, the value of the original variable can be modified through this address.

For example:

 
 

c复制代码

void add(int *a, int *b) {
*a = *a + *b;
}

In this example, a and  b are pointers to integers. Inside the function  add , we can   modify   the value of the variables in the function through pointers a and  . 3. Array parameter (Pass by Array): Array parameter is a special form of reference parameter. In this method, the function receives a pointer to the first element of the array. Therefore, inside the function, the elements of the array can be accessed and modified through this pointer.bmain

For example:

 
 

c复制代码

void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}

In this example, arr it's a pointer to an array of integers, size but the size of the array. Inside the function  printArray , we can  arr access and print the elements of the array through pointers.
4. Pass by Function Pointer: In this method, the function receives a pointer to other functions. Therefore, within the function, other functions can be called through this function pointer.

For example:

 
 

c复制代码

void (*fp)(int);
void add(int a, int b) {
printf("%d\n", a + b);
}
void callFunction(int a, int b, void (*fp)(int)) {
fp(a + b);
}
int main() {
callFunction(2, 3, add);
return 0;
}

In this example, fp it's a pointer to a function. Inside the function  callFunction , we can  fp call other functions through this function pointer.

Guess you like

Origin blog.csdn.net/qq_50942093/article/details/132913595