当前位置:网站首页>Application of learning diary 5-c language function
Application of learning diary 5-c language function
2022-07-20 08:34:00 【Astray King】
Definition and declaration of functions
1. Function definition : A function is a code module that performs a specific function , Usually there are parameters , There can be no parameters ; Usually there is a return value , It can also be null .
2. General form :< data type >< The name of the function >(< Formal parameter description >)
The function name is an identifier , Conform to the naming rules ( In numbers and letters _ form , But don't start with a number , It can't be a keyword )
3. The data type is the type of the return value of the whole function , When no return value is needed , Data types are available void;
4. The advantages of functions : Code reuse , modularization , Easy to maintain , read .
5. Formal parameters can be variables of any type , The parameters are separated by commas , The actual values of these formal parameters will be given when making a function call .
6. Functions cannot be defined repeatedly , And cannot nest definitions .
7. The called function must be a declared function , Or the position of the called function is before the calling function .
8. If the function is not declared before calling it , Then the compiling system will put the function form encountered for the first time ( Function definition or function out of use ) Declaration as a function , And the type of the return value of the function is defaulted to int;
9. Function call : Nested calls , Recursively call ( Pay attention to the ending conditions ).
10. The disadvantages of global variables : Easily modified , Long life cycle , Occupy memory , Easily shielded ( Reassigning values in functions results in overwriting ).
11 Scope :1.{} The inside is local ;2 overall situation : Define to the end of the program
The return value of the function
1. The return value of a function can only be passed through return Statement returns the main function , And can only return one value ;
2. The type of the function return value should be consistent with the data type defined by the function , If it's not the same , Then the type in the function definition shall prevail , Automatic type conversion , no return value , Can be defined as void type .
Some examples
1. Find with function 1+2+.......+n And
#include <stdio.h>
int sum(int n)
{
if(n==0)
return 0;
return n+sum(n-1);// Recursively call
}
int main(int argc, char *argv[])
{
int a;
a=sum(100);
printf("%d",a);
return 0;
}
2. seek X^n Value
#include <stdio.h>
double work(double x,int n)
{
int i;
double m=x;
for(i=1;i<n;i++)
{
x=m*x;
}
return x;
}
int main(int argc, char *argv[])
{
double s;
double c;
int d;
scanf("%lf%d",&c,&d);
s=work(c,d);
printf("%f",s);
return 0;
}
3. Input the total number of chickens and rabbits and the total number of feet , Find out how many chickens and rabbits there are ?
#include <stdio.h>
void number(int x,int y)
{
int i,j;
for(i=0;i<=x;i++)
{
for(j=0;j<=x;j++)
{
if(i+j==x&&2*i+4*j==y)
{
printf(" The number of Rabbits =%d Number of chickens =%d\n",j,i);
return ;
}
}
}
printf("invail!\n");
return;
}
int main(int argc, char *argv[])
{
int a,b,m;
printf(" Please enter the total number of pieces and feet \n");
scanf("%d%d",&a,&b);
number(a,b);
return 0;
}
4. Write a function to judge leap years
#include <stdio.h>
int leapyear(int x)// The function of judging leap years
{
if(x%4==0&&(x%100!=0||x%400==0))
return 1;
}
int main(int argc, char *argv[])
{
int x,i;
printf("please input year\n");
scanf("%d",&x);
i=leapyear(x);
if(i==1)
printf("%d is a leap year\n",x);
else
printf("%d is not a leap year\n",x);
return 0;
}
5. Write a function to judge prime numbers
#include <stdio.h>
int prime(int x)// Judge the prime function
{
int i;
for(i=2;i<x;i++)
{
if(x%i!=0)
{
return 1;
}
}
return 0;
}
int main(int argc, char *argv[])
{
int x,a;
printf("please input a number\n");
scanf("%d",&x);
a=prime(x);
if(a==1)
printf("the number is a prime number\n");
else
printf("the number is not a prime number\n");
return 0;
}
6. Guess the number game
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
int number;
int guess;
int i;
srand((unsigned)time(NULL));
number=rand()%100;
number++;
printf("guess number game!\n");
printf("the range of number is 1-100\n");
for(i=0;;i++)
{
printf("please input number\n");
scanf("%d",&guess);
if(guess>number)
{
printf(" big \n");
}
else if(guess<number)
printf(" The small \n");
else
{
printf("victory!\n");
break;
}
}
return 0;
}
7. seek N The class of
#include <stdio.h>
int factorial(int n)//FOR Cycle factorial
{
int i;
for(i=n;i>1;i--)
{
n=n*(i-1);
}
return n;
}
int funtion(int n)// Recursively factoring
{
if(n==1)
return 1;
return n*funtion(n-1);
}
int main(int argc, char *argv[])
{
int n;
int sum;
printf("please input n\n");
scanf("%d",&n);
//sum=factorial(n);
sum=funtion(n);
printf("%d The factorial of is %d\n",n,sum);
return 0;
}
8. Print 1000-2000 The leap year of the year
#include <stdio.h>
void leapyear(int n)
{
int i;
for(i=n;i<=2000;i++)
{
if(i%4==0&&(i%100!=0||i%400==0))
printf("%-8d",i);
}
return;
}
int main(int argc, char *argv[])
{
leapyear(1000);
return 0;
}
9. Calculation 1!+2!+3!+...........+N!
#include <stdio.h>
int sume(int n)
{
int i,j;
int sum=1;
int S=0;
for(i=1;i<=n;i++)
{
sum=sum*i;
S=S+sum;
}
return S;
}
int main(int argc, char *argv[])
{
int n,sum;
printf(" Enter the order multiplier you want \n");
scanf("%d",&n);
sum=sume(n);
printf(" The sum of factorials is %d\n",sum);
return 0;
}
10. seek 1!-2!+3!-............+N!
#include <stdio.h>
int sume(int n)
{
int i,j;
int sum=1;
int S=0;
for(i=1;i<=n;i++)
{
sum=sum*i;
S=S+sum;
sum=(-1)*(sum);
}
return S;
}
int main(int argc, char *argv[])
{
int n,sum;
printf(" Enter the order multiplier you want \n");
scanf("%d",&n);
sum=sume(n);
printf(" The sum of factorials is %d\n",sum);
return 0;
}
11. Landing system , Exit the system after three errors
#include <stdio.h>
#define password 1234
int system(int n)
{
int i=1;
int mm;
while(i<=n)
{
printf("input your password\n");
scanf("%d",&mm);
if(mm==password)
{
return 1;
}
i++;
}
return 0;
}
int main(int argc, char *argv[])
{
int a;
a= system(3);
if(a==1)
printf("login succeeded!\n");
else
printf("your password is wrong!login out system\n");
return 0;
}
12. Find the greatest common divisor of two numbers
#include <stdio.h>
int conven(int x,int y)
{
int i,r;
for(i=1;i<=x&&i<=y;i++)
{
if(x%i==0&&y%i==0)
r=i;
}
return r;
}
int main(int argc, char *argv[])
{
int x,y,a;
printf("input two number\n");
scanf("%d%d",&x,&y);
a=conven(x,y);
printf("%d and %d The greatest common divisor of is %d",x,y,a);
return 0;
}
边栏推荐
- yolov3的GUI界面(3)--解决out of memory问题,新增摄像头检测功能
- 有趣的扫雷游戏,C语言编程实现
- 三子棋(N子棋)C语言编程实现,超详细讲解
- 八皇后问题,秒懂递归回溯(有图详解|c语言)
- Consolidate and review the pointers and callback functions pointing to the function pointer array
- C程序中的函数递归
- Basic use of JWT (JSON web token)
- Yolov5 realizes flame and smoke detection
- 更易上手的C语言入门级芝士 (1) 数据类型、变量和常量、字符串+转义字符+注释(超详细)
- [yolov5 realizes mask and face detection]
猜你喜欢
Yolo series target detection data set
STM32-仿真调试时的SystemInit陷阱
Pytorch target detection competition (I) data analysis
堆堆排序及堆的相关操作
Pointer operation exercises and string functions
仿联系人的排序
Function recursion in C program
Basic introduction to multithreading (with sample code)
Basic use of JWT (JSON web token)
Skillfully solve Young's matrix
随机推荐
MySQL data specifies the field name and field remarks of the data table through SQL query
第五十八篇:VS调试出现“覆盖。。。。是/N:否/A:全部)??”
Openwrt manually installs the netdata plug-in
第六十四篇:error LNK2019: 无法解析的外部符号 cvRound
yolov3的GUI界面(简易,图片检测)
Skillfully solve Young's matrix
更易上手的C语言入门级芝士 (1) 数据类型、变量和常量、字符串+转义字符+注释(超详细)
第七十四篇:机器学习优化方法及超参数设置综述
练习题(1)创建一个集合c1,存放元素“one“,“two“,“three“
Pointer in C language (learning experience)
Pytorch target detection coco API explanation data generation
Yolov5 détection des oiseaux
STM32-使用定时器做延时函数时遇到的坑
ZABBIX sender adds a custom monitoring item -- Ping to destination link monitoring -- bat script cycle
练习提升C语言-1
yolov3的Gui界面(2)--美化页面+输出识别物体名称及数量
指针数组和数组指针有什么区别?
标准IO与文件IO
学习日记3-数据的输入输出
什么是多线程