当前位置:网站首页>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;
}
边栏推荐
- Character functions and string functions
- Win10中用VS2019编译live555
- C程序中的函数递归
- 第七十五篇:学术论文写作技巧
- Pytorch target detection data enhances the mixing of cutmix and mixup
- 4000 words, let you understand recursion and its example practice (C language, with pictures)
- What if the game needs to be reinstalled after the steam folder is moved
- Pytorch target detection coco API explanation data generation
- 字符函数和字符串函数
- 第七十四篇:机器学习优化方法及超参数设置综述
猜你喜欢
The longest common subsequence of order 2 and order n
Interval coverage problem
Pointer in C language (learning experience)
C language program environment and preprocessing
二阶及N阶最长公共子序列
SSM notes
Pytorch target detection coco API explanation data generation
第六十二篇:win10上运行VS程序出现蓝屏崩溃
详解全排列问题(不同的解法的不同输出结果)
Custom type: structure, bit segment, enumeration, union
随机推荐
4000字,让你明白递推及其例题做法(C语言,有图)
练习提升C语言-1
The project is lost after Jenkins restarts (including the download address of Jenkins plugins)
Pointer operation exercises and string functions
基于STM32F030的ADC功能实现
C language structure knowledge sharing
Chapter 58: vs debugging appears "coverage.... yes /n: no /a: all)
更易上手的C语言入门级芝士 (1) 数据类型、变量和常量、字符串+转义字符+注释(超详细)
Pytorch target detection coco API explanation data generation
YOLOv5飞鸟检测
C语言中的指针(学习心得)
编程实现猜密码游戏
数据在内存中存储是怎样的?
Heap sorting and heap related operations
Skillfully solve Young's matrix
yolov3训练自己的数据集
递归回溯—走迷宫
解决快速索引栏挤压的问题
Based on STM32F103, play songs with buzzer
C语言中会节省空间的“位段“