C Language Lesson 8 —– Continuation of Function Definition and Use

Author’s Foreword

Personal homepage:: small page

gitee page: David Qin

A small blogger who loves to share Welcome all the cuties to learn from

____________________________________________________________

Table of Contents

1. review

Library Functions

custom function

function parameters

function call

Nested calls and chained access of functions

2. Function declaration and definition

3. Function recursion

________________________________________________________

Interstitial knowledge

1. In the previous part, I simply drew the approximate memory distribution of C language

2. The function has an external linkage attribute

In the last blog, we briefly introduced the definition, classification, and call of functions. Let’s briefly review them

Library functions

Simply put, it is a common function provided by the c language compiler manufacturer, which supports portability and improves the efficiency of the program, which is convenient for programmers to carry out software development

http://www.cplusplus.com

custom function

ret_type fun_name(para1, * )
{
 statement;//statement item
}
ret_type return type
fun_name function name
para1 function parameter

A code block with certain functions created according to the standard of C language

function parameters

1. Actual parameters (actual parameters)

Variables or other values passed in by the calling function

2. Formal parameters (parameters)

Variables defined when customizing functions

function call

Functions must be declared before they are used

1. Call by value

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.

2. Call by address

Call by reference 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 will affect external variables, and can establish a real connection between the function and the variables outside the function, that is, the variables outside the function can be directly manipulated inside the function.

Nested calls and chained access of functions

Nested calls

To put it simply, it is to call the function in the function. Note that the function cannot be defined in the function

Chain access


Use the return value of one function as an argument to another function.

Let’s go to the last point of the function,

Function declaration and definition

Function declaration:

1.
Tell the compiler what a function is called, what its parameters are, and what its return type is. But whether it exists specifically, the function

The statement does not decide.

2.
The declaration of a function generally appears before the use of the function. to be satisfied
declare before use
.

3.
Function declarations are generally placed in header files

Function definition

General functions are defined in the c file and declared in the header file

The realization of the link of the integrated development environment file of vs2022

The benefits of splitting the code into .c .h files

1. Multi-person collaboration

2. Code Protection

Function recursion (recursion plus regression)

The programming technique that the program calls itself is called recursion, which converts a large and complex problem layer by layer into a smaller-scale problem similar to the original problem to solve,

Recursive strategy

Only a small number of programs can describe the multiple repeated calculations required in the problem-solving process, which greatly reduces the amount of code in the program. The main way of thinking about recursion is: make big things small

Simple recursion 1:

#include<stdio.h>
int main()
{
printf("1\
");
main();
return 0;
}

Two necessary conditions for recursion

1.
There are constraints, and when this constraint is met, the recursion will not continue.

2. After each recursive call, it gets closer and closer to this constraint.

exercise 1

Enter:
1234
, output
1 2 3 4

#include<stdio.h>
void Print(int a)
{
printf("%d", a % 10);
if (a / 10)
{
Print(a / 10);
}
\t
}
int main()
{
int n = 0;
scanf("%d", &n);
Print(n);
}

The reason why the function can achieve recursion is that a function stack frame (an area on the memory) is created when the function is called, the function call starts, the function stack frame is created, the function call ends, and the stack frame is destroyed

This is the simple process of creating a stack frame with n = 1234

Write a function that does not allow the creation of temporary variables to find the length of the string.

#include <stdio.h>
//int strlen(char arr[])
//{
// if (arr[0] != '\0')
// {
// return 1 + strlen(arr + 1);
// }
// return 0;
//}
int main()
{
//Recursive and non-recursive implement strlen respectively
\t
char arr[30];
printf("Enter string:>");
scanf("%s", &arr);
//int sum = strlen(arr);
\t
int sum = 0;
\t
int i = 0;
for (i = 0; ; i ++ )
{
if (arr[i] != '\0')
{
sum + + ;
continue;
}
break;
}

printf("%d", sum);
return 0;
}

Recursion and iteration

When we can’t solve it using recursion, we can think about using iteration. Let’s write the factorial of n below.

#include <stdio.h>
int fac(a)
{
if (a == 1)
{
return 1;
}
return a * fac(a - 1);
}
int main()
{
int a = 0;
printf("Enter a number:>");
scanf("%d", &a);
int num = fac(a);
printf("%d", num);
return 0;
}

The above method takes a lot of time to calculate large values, mainly due to stack overflow.

So we have to consider iterating

#include<stdio.h>
int main()
{
int a = 0;
scanf("%d", &a);
int i = 0;
int sum = 0;
int num = 1;
for (i = 1; i <= a; i ++ )
{
sum = i * num;
num = sum;
}
printf("%d", sum);

return 0;
}

There will be no stack overflow when iterating

Seek number
no
a Fibonacci number.

#include<stdio.h>
int main()
{
int a = 0;
scanf("%d", &a);
int i = 1;
int j = 1;
int sum = 0;
if (a <= 2)
{
printf("%d", i);
return 0;
}
while (a >= 3)
{
sum = i + j;
i = j;
j = sum;
a--;
}
printf("%d", sum);
return 0;
}

Summary:

This is the end of the introduction of the function here. If you don’t understand, you can chat with me privately.