当前位置:网站首页>C language · function
C language · function
2022-07-18 02:52:00 【Wmpreturn】
Catalog
Function declaration and definition
The relationship between the main function and other functions
Function declaration and definition
The definition of function refers to the establishment of function function , Including specifying function names 、 Function value type 、 Formal parameters and their types 、 Function body, etc , It's a complete 、 Independent functional units .
The function declaration is used to put the name of the function 、 Function type and parameter type 、 The number and order inform the compiling system so that when the function is called, the compiling system can correctly identify the function and check whether the call is legal .
( Implicitly declare :C There are several declared type names in the language that can be omitted . for example : If the function name does not explicitly declare the type of the return value , Then it returns integer by default ; Implicit declaration is not a good habit ).
Definition :
Function name type identifier Function name ( List of formal parameters , With or without parameters , Each formal parameter must define the parameter type separately )
{
Declaration part
Statement part
}
Description of arguments and formal parameters :
Define the formal parameters specified in the function , If there is no function call , It will not occupy the storage unit in memory . Only when a function call occurs , The formal parameters in the function we define will be allocated memory units . At the end of the call , Memory units occupied by formal parameters will also be released . The parameters passed by the main function are actual parameters , In terms of quantity, it should correspond to formal parameters one by one , Arguments can be constant , Variable , expression , But they are required to have definite values . The types of formal parameters and arguments should be the same or assignment compatible . Data transfer from arguments to formal parameters is one-way " Value passed ", Only arguments can be passed to formal parameters .
Global variables : In all functions ( Include main function ) Outside , The defined variable is a global variable . Global variables i Stored in data segment , therefore main Functions and print Functions are visible . Global variables do not disappear when a function is executed , It is always effective during the execution of the whole process . If the local variable has the same name as the global variable , Take the principle of proximity , That is, the value actually obtained and modified is the value of the local variable .
The relationship between the main function and other functions
When the program is compiled, it is compiled in the unit of source program file rather than in the unit of function .
The calling relationship between functions is : Call other functions by the main function , Other functions can also call each other , The same function can be called any time by one or more functions .C Execution of language from main Function to , If in main If you call other functions in the function, you will return to main Function , stay main Function to end the running of the whole program .
All functions are parallel , In other words, the function is defined separately , And independent of each other . One function does not belong to another function , Functions cannot be nested . Functions can call each other , But you can't call the main function , The main function is called by the system .

example :
#include<stdio.h>
// Function call
int printstar(int i)// Function declaration , Declare functions printstar,i Is the formal parameter
{
printf("*****************\n");
printf("printstar %d\n",i);
return i+3;
}
void print_message()// You can call printstar
{
printf("how do you do \n");
printstar(3);// Function call
}
int main()
{
int a = 10;
a = printstar(a);
print_message();
printstar(a);
return 0;
}
Running results :
*****************
printstar 10
how do you do
*****************
printstar 3
*****************
printstar 13
?????? This output is not understood .
Recursively call
subject : If there is n A stair , You can only go to 1 A step or 2 A stair , Please walk to the No n There are several ways to walk a step ?
analysis : If there is 3 A stair , So the total is 3 Seed walking method : The first kind of each time 1 A stair , On 3 Time ; The second is to go first 2 A stair , And then on 1 A stair ; The third is to go first 1 A stair , And then on 2 A stair .
#include<stdio.h>
//n Recursive call implementation of factorial of
/**
* @brief
* If there is n A stair , You can only go to 1 A step or 2 A stair , Please walk to the No n There are several ways to walk a step ?
* analysis : If there is 3 A stair , So the total is 3 Seed walking method : The first kind of each time 1 A stair , On 3 Time ;
* The second is to go first 2 A stair , And then on 1 A stair ; The third is to go first 1 A stair , And then on 2 A stair .
*/
// n = 1 specie = 1
// n = 2 specis = 2 1+1= 2
// n = 3 specis = 3 1+ 2 =3
// n = 4 specis = 5 2+3 =5
// n = 5 specis = 6 5+ 3= 8
// 1 1 1 1 1 1 2 1 2 1 2 1 1 2 2
// 11111 1112 1121 1211 2111 122 212 221
// Calculation k There are several ways to walk a step
int specie(int n)
{
if (n == 1)
{
return 1;
}
else if (n == 2)
{
return 2;
}
else
{
return specie(n-1)*specie(n-2);
}
}
int main()
{
int n,ret;// Altogether n A stair
printf(" Please enter the number of stairs :\n");
scanf("%d",&n);
ret = specie(n);
printf(" Altogether %d A way to climb stairs \n",specie(n));
return 0;
}
Console :
Please enter the number of stairs :
6
Altogether 32 A way to climb stairs
边栏推荐
猜你喜欢

「开源摘星计划」Harbor高可用集群设计及部署(实操+视频),基于离线安装方式

Why is it difficult for girls to learn programming? Is it the wrong way of thinking or the wrong way of learning?

03 key control LED

Nacos as the registration and discovery center and configuration center of microservice architecture

PowerDesigner installation tutorial

Practice of kotlin stateflow search function DB + network

阿里云物联网平台搭建

02 STM32CubeMX新建工程

使用USBasp 通过ICSP模式给arduino UNO烧写程序

558. 四叉树交集 : 简单递归运用题
随机推荐
使用 Terraform 在阿里云上快速部署 MQTT 集群
Vector
Yunna dynamic loop monitoring system realizes unmanned rapid fault handling
实现意识的远程直接电磁通信,东大团队联合新加坡国大等构建电磁脑机超表面,有望成为全新通信范式
2022-04-20 unity entry 6 - light source components
02 STM32CubeMX新建工程
Difference between idea smart checkout and force checkout
从零实现深度学习框架——GloVe
ESP32的串口通信(以中断和看门狗的方式进行)
Landing DDD (7) - some misunderstandings in tactical design
BC20 AT指令测试
Wu Enda writes: an important step in developing AI career -- project practice
MQTT---Connect
Meta announced the launch of make-a-scene: AI image generation can be controlled based on text and sketches
[design topics] project summary of graduation project based on STM32 - 350 cases
Fragment (III) using fragment in viewpager
Binary search (bottom)
字符串拼接
CCF 202012-1 期末预测之安全指数
[google] goodbye SharedPreferences embrace jetpack datastore