当前位置: 首页 > news >正文

做平面设计素材的哪个网站好天津网络推广公司

做平面设计素材的哪个网站好,天津网络推广公司,汽车最专业的网站建设,上海做网站品牌公司目录 一.函数是什么? 二.C语言中函数的分类 2.1库函数 2.2自定义函数 三.函数的参数 3.1实际参数(实参) 3.2 形式参数(形参) 四.函数的调用 4.1 传值调用 4.2 传址调用 五. 函数的嵌套调用和链式访问 5.1 嵌套调用 5…

目录

一.函数是什么?

二.C语言中函数的分类

 2.1库函数

2.2自定义函数

三.函数的参数

3.1实际参数(实参)

3.2 形式参数(形参)

四.函数的调用

4.1 传值调用

4.2 传址调用

五. 函数的嵌套调用和链式访问

5.1 嵌套调用

5.2 链式访问 

 六.函数的声明和定义

6.1 函数声明:

6.2 函数定义:

7. 函数递归

后续我也会更新递归和回溯的算法7.1 什么是递归?

7.2 递归的两个必要条件


一.函数是什么?

数学中我们常见到函数的概念。但是你了解C语言中的函数吗?
维基百科中对函数的定义:子程序
在计算机科学中,子程序(英语:Subroutine, procedure, function, routine, method,
subprogram, callable unit),是一个大型程序中的某部分代码, 由一个或多个语句块组
成。它负责完成某项特定任务,而且相较于其他代 码,具备相对的独立性。
一般会有输入参数并有返回值,提供对过程的封装和细节的隐藏。这些代码通常被集成为软
件库。

二.C语言中函数的分类

        1库函数        

        2.自定义函数

 2.1库函数

为什么会有库函数?
1. 我们知道在我们学习C语言编程的时候,总是在一个代码编写完成之后迫不及待的想知道结果,想
把这个结果打印到我们的屏幕上看看。这个时候我们会频繁的使用一个功能:将信息按照一定的格
式打印到屏幕上(printf)。
2. 在编程的过程中我们会频繁的做一些字符串的拷贝工作(strcpy)。
3. 在编程是我们也计算,总是会计算n的k次方这样的运算(pow)。
像上面我们描述的基础功能,它们不是业务性的代码。我们在开发的过程中每个程序员都可能用的到,

为了支持可移植性和提高程序的效率,所以C语言的基础库中提供了一系列类似的库函数,方便程序员
进行软件开发。
那怎么学习库函数呢?
C和C++可以使用www.cplusplus.com

简单的总结,C语言常用的库函数都有:
IO函数
字符串操作函数
字符操作函数
内存操作函数
时间/日期函数
数学函数
其他库函数

比如strcpy

int main()
{//字符串拷贝的时候//hello bitchar arr1[20] = { 0 };char arr2[] = "hello bit";//把arr2中的字符串拷贝到arr1中char* ret = strcpy(arr1, arr2);printf("%s\n", ret);return 0;
}

2.2自定义函数

如果库函数能干所有的事情,那还要程序员干什么?
所有更加重要的是自定义函数。
自定义函数和库函数一样,有函数名,返回值类型和函数参数。
但是不一样的是这些都是我们自己来设计。这给程序员一个很大的发挥空间。
函数的组成:

ret_type fun_name(para1, * )
{
statement;//语句项
}
ret_type 返回类型
fun_name 函数名
para1   函数参数

函数返回类型不写,默认返回INT类型

我们写一个可以找出两个数中最大值的函数

#include <stdio.h>
//get_max函数的设计
int get_max(int x, int y)
{
return (x>y)?(x):(y);
}
int main()
{
int num1 = 10;
int num2 = 20;
int max = get_max(num1, num2);
printf("max = %d\n", max);
return 0;
}

三.函数的参数

3.1实际参数(实参)

真实传给函数的参数,叫实参。
实参可以是:常量、变量、表达式、函数等。
无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形
参。

3.2 形式参数(形参)

形式参数是指函数名后括号中的变量,因为形式参数只有在函数被调用的过程中才实例化(分配内
存单
元),所以叫形式参数。形式参数当函数调用完成之后就自动销毁了。因此形式参数只在函数中有
效。

代码对应的内存分配如下 

这里可以看到 Swap1 函数在调用的时候, x , y 拥有自己的空间,同时拥有了和实参一模一样的内容。
所以我们可以简单的认为:形参实例化之后其实相当于实参的一份临时拷贝 ,只要不改变原来的实际参数,可以不必传址调用

四.函数的调用

4.1 传值调用


函数的形参和实参分别占有不同内存块,对形参的修改不会影响实参。

4.2 传址调用


传址调用是把函数外部创建变量的内存地址传递给函数参数的一种调用函数的方式。
这种传参方式可以让函数和函数外边的变量建立起真正的联系,也就是函数内部可以直接操
作函数外部的变量。

写一个函数,实现一个整形有序数组的二分查找

int binary_search(int arr[], int k, int sz)
{int left = 0;int right = sz - 1;while (left<=right){int mid = (left + right) / 2;if (arr[mid] < k){left = mid + 1;}else if (arr[mid] > k)i{right = mid - 1;}else{return mid;}}return -1;//找不到
}int main()
{int arr[] = { 1,2,3,4,5,6,17,18,19,20,21 };//10//0~9int k = 21;//找到了就返回下标//找不到返回-1//计算数组的元素个数// //printf("%d\n", sizeof(arr));//40,这里计算整个数组的大小,单位是字节//printf("%d\n", sizeof(arr[0]));//4,这里计算数组第一个元素的大小,单位是字节int sz = sizeof(arr) / sizeof(arr[0]);int pos = binary_search(arr, k, sz);if (-1 == pos)printf("找不到\n");elseprintf("找到了,下标是:%d\n", pos);return 0;
}

五. 函数的嵌套调用和链式访问


函数和函数之间可以根据实际的需求进行组合的,也就是互相调用的。

5.1 嵌套调用

#include <stdio.h>
void new_line()
{
printf("hehe\n");
}
void three_line()
{int i = 0;
for(i=0; i<3; i++){new_line();}
}
int main()
{
three_line();
return 0;
}

函数可以嵌套调用,但是不能嵌套定义。

5.2 链式访问 

把一个函数的返回值作为另外一个函数的参数。

#include <stdio.h>
#include <string.h>
int main()
{char arr[20] = "hello";
int ret = strlen(strcat(arr,"bit"));//这里介绍一下strlen函数
printf("%d\n", ret);
return 0;
}
#include <stdio.h>
int main()
{printf("%d", printf("%d", printf("%d", 43)));//结果是啥?//注:printf函数的返回值是打印在屏幕上字符的个数return 0;
}

printf返回值是打印的字符个数,所以

 六.函数的声明和定义

6.1 函数声明:


1. 告诉编译器有一个函数叫什么,参数是什么,返回类型是什么。但是具体是不是存在,函数
声明决定不了。
2. 函数的声明一般出现在函数的使用之前。要满足先声明后使用。
3. 函数的声明一般要放在头文件中的。

6.2 函数定义:


函数的定义是指函数的具体实现,交待函数的功能实现。

test.h的内容
放置函数的声明

#ifndef __TEST_H__
#define __TEST_H__
//函数的声明
int Add(int x, int y);
#endif //__TEST_H__

test.c的内容
放置函数的实现

#include "test.h"
//函数Add的实现
int Add(int x, int y)
{
return x+y;
}

主函数所在文件即可使用

#include"test.h"

#include<stdio.h>

int main()

{

        int a,b=0;

        scanf("%d%d",&a,&b);

        int ret=Add(a,b);

        printf("%d“,ret);

        return 0;

}

分模块写代码更规范,也可以了解静态库等概念做代码隐藏

7. 函数递归

后续我也会更新递归和回溯的算法
7.1 什么是递归?


程序调用自身的编程技巧称为递归( recursion)。
递归做为一种算法在程序设计语言中广泛应用。 一个过程或函数在其定义或说明中有直接或间接
调用自身的
一种方法,它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,
递归策略
只需少量的程序就可描述出解题过程所需要的多次重复计算,大大地减少了程序的代码量。
递归的主要思考方式在于:把大事化小

7.2 递归的两个必要条件


存在限制条件,当满足这个限制条件的时候,递归便不再继续。
每次递归调用之后越来越接近这个限制条件。

递:递推

归:回归

7.2.1 练习1:
接受一个整型值(无符号),按照顺序打印它的每一位。
例如:
输入:1234,输出 1 2 3  4

#include <stdio.h>
void print(int n)
{
if(n>9)
{
print(n/10);
}
printf("%d ", n%10);
}
int main()
{
int num = 1234;
print(num);
return 0;
}

7.2.2 练习2:

编写函数不允许创建临时变量,求字符串的长度

int my_strlen(char* str)
{if (*str != '\0')return 1 + my_strlen(str + 1);elsereturn 0;
}int main()
{char arr[] = "bit";//[b i t \0]//数组名其实是数组首元素的地址//int len = my_strlen(arr);printf("%d\n", len);return 0;
}

7.3.1 练习3:
求n的阶乘。(不考虑溢出)

int factorial(int n)
{
if(n <= 1)
return 1;
else
return n * factorial(n-1);
}

7.3.2 练习4:
求第n个斐波那契数。(不考虑溢出)

int fib(int n)
{
if (n <= 2)    
return 1;elsereturn fib(n - 1) + fib(n - 2);
}

但是我们发现有问题;
在使用 fib 这个函数的时候如果我们要计算第50个斐波那契数字的时候特别耗费时间。
使用 factorial 函数求10000的阶乘(不考虑结果的正确性),程序会崩溃

为什么呢?
我们发现 fib 函数在调用的过程中很多计算其实在一直重复。


 

那我们如何改进呢?
在调试 factorial 函数的时候,如果你的参数比较大,那就会报错: stack overflow(栈溢出)
这样的信息。
系统分配给程序的栈空间是有限的,但是如果出现了死循环,或者(死递归),这样有可能导致一
直开辟栈空间,最终产生栈空间耗尽的情况,这样的现象我们称为栈溢出。
那如何解决上述的问题:
1. 将递归改写成非递归。

2. 使用static对象替代 nonstatic 局部对象。在递归函数设计中,可以使用 static 对象替代
nonstatic 局部对象(即栈对象),这不
仅可以减少每次递归调用和返回时产生和释放 nonstatic 对象的开销,而且 static 对象还可以保
存递归调用的中间状态,并且可为各个调用层所访问。

比如,下面代码就采用了,非递归的方式来实现: 

//求n的阶乘
int factorial(int n)
{int result = 1;while (n > 1){result *= n ;n -= 1;}return result;
}
//求第n个斐波那契数
int fib(int n)
{int result;int pre_result;int next_older_result;result = pre_result = 1;
while (n > 2){n -= 1;next_older_result = pre_result;pre_result = result;result = pre_result + next_older_result;}return result;
}

字符串逆序:

//非递归
void reverse_string(char* arr)
{char *left = arr;char *right = arr+strlen(arr)-1;while(left<right){char tmp = *left;*left = *right;*right = tmp;left++;right--;}
}
/*
递归方式:
对于字符串“abcdefg”,递归实现的大概原理:1. 交换a和g,2. 以递归的方式逆置源字符串的剩余部分,剩余部分可以看成一个有效的字符串,再以类似的方式逆置
*/
void reverse_string(char* arr)
{int len = strlen(arr);char tmp = *arr;*arr = *(arr+len-1);*(arr+len-1) = '\0';if(strlen(arr+1)>=2)reverse_string(arr+1);*(arr+len-1) = tmp;
}

1. 许多问题是以递归的形式进行解释的,这只是因为它比非递归的形式更为清晰。
2. 但是这些问题的迭代实现往往比递归实现效率更高,虽然代码的可读性稍微差些。
3. 当一个问题相当复杂,难以用迭代实现时,此时递归实现的简洁性便可以补偿它所带来的运行时开
销。


文章转载自:
http://dinncoseafloor.tqpr.cn
http://dinncoagriology.tqpr.cn
http://dinncoidiomorphically.tqpr.cn
http://dinncostingray.tqpr.cn
http://dinncocribellum.tqpr.cn
http://dinncokinswoman.tqpr.cn
http://dinncoelectrotherapeutical.tqpr.cn
http://dinncoterminable.tqpr.cn
http://dinncodividable.tqpr.cn
http://dinncorecruitment.tqpr.cn
http://dinncoreface.tqpr.cn
http://dinncoaiff.tqpr.cn
http://dinncozagreb.tqpr.cn
http://dinnconeighbouring.tqpr.cn
http://dinncochit.tqpr.cn
http://dinncofilibusterer.tqpr.cn
http://dinncomawlamyine.tqpr.cn
http://dinncovex.tqpr.cn
http://dinncocrosswind.tqpr.cn
http://dinncofroglet.tqpr.cn
http://dinncoprosy.tqpr.cn
http://dinncoexpiratory.tqpr.cn
http://dinncotavr.tqpr.cn
http://dinncopamphlet.tqpr.cn
http://dinncoticket.tqpr.cn
http://dinncoseism.tqpr.cn
http://dinncokhodzhent.tqpr.cn
http://dinncoroseanna.tqpr.cn
http://dinncoteachy.tqpr.cn
http://dinncopharmacology.tqpr.cn
http://dinncoflacon.tqpr.cn
http://dinncounpuzzle.tqpr.cn
http://dinncosourpuss.tqpr.cn
http://dinncoalcalde.tqpr.cn
http://dinncoamongst.tqpr.cn
http://dinncorockery.tqpr.cn
http://dinncodetectable.tqpr.cn
http://dinncosonet.tqpr.cn
http://dinncoscattering.tqpr.cn
http://dinncokrummhorn.tqpr.cn
http://dinncolinzertorte.tqpr.cn
http://dinncoputative.tqpr.cn
http://dinncounseal.tqpr.cn
http://dinncofarrier.tqpr.cn
http://dinncoideologist.tqpr.cn
http://dinncotheomorphic.tqpr.cn
http://dinncoexiguity.tqpr.cn
http://dinncodiplopy.tqpr.cn
http://dinncooverswing.tqpr.cn
http://dinncodaring.tqpr.cn
http://dinncothigmotropism.tqpr.cn
http://dinncosori.tqpr.cn
http://dinncoblender.tqpr.cn
http://dinncotravolater.tqpr.cn
http://dinncoindoctrination.tqpr.cn
http://dinncotube.tqpr.cn
http://dinncocinchonidine.tqpr.cn
http://dinncoroyalist.tqpr.cn
http://dinncolumpily.tqpr.cn
http://dinncoluxon.tqpr.cn
http://dinncogimlety.tqpr.cn
http://dinncopermittivity.tqpr.cn
http://dinncodemocratization.tqpr.cn
http://dinncowhydah.tqpr.cn
http://dinncotref.tqpr.cn
http://dinncomadras.tqpr.cn
http://dinncorindless.tqpr.cn
http://dinncocoombe.tqpr.cn
http://dinncotrainmaster.tqpr.cn
http://dinncocortege.tqpr.cn
http://dinncoedh.tqpr.cn
http://dinncosophomoric.tqpr.cn
http://dinncofremdly.tqpr.cn
http://dinncohausfrau.tqpr.cn
http://dinncoexcel.tqpr.cn
http://dinncounpathed.tqpr.cn
http://dinncoextraversion.tqpr.cn
http://dinncosonnet.tqpr.cn
http://dinncoticklish.tqpr.cn
http://dinncomelchisedech.tqpr.cn
http://dinncologin.tqpr.cn
http://dinncohyalinization.tqpr.cn
http://dinncoaxiomatize.tqpr.cn
http://dinncochalcography.tqpr.cn
http://dinncohagiography.tqpr.cn
http://dinncopresentative.tqpr.cn
http://dinncoinopportune.tqpr.cn
http://dinncoglycine.tqpr.cn
http://dinncoumbrella.tqpr.cn
http://dinncohydrocolloid.tqpr.cn
http://dinncosinclair.tqpr.cn
http://dinncosupervisorship.tqpr.cn
http://dinncohover.tqpr.cn
http://dinncooutsmart.tqpr.cn
http://dinncojinan.tqpr.cn
http://dinncoavidity.tqpr.cn
http://dinncohalidome.tqpr.cn
http://dinncouhf.tqpr.cn
http://dinncomoly.tqpr.cn
http://dinncorolling.tqpr.cn
http://www.dinnco.com/news/92013.html

相关文章:

  • 如何让自己做的网站让别人看到视频号推广
  • 怎样在门户网站做 推广中国seo网站
  • 网站改版 升级的目的是什么意思百度排行榜风云榜小说
  • 无锡注册公司流程和费用多少网站优化排名易下拉软件
  • 网站二级域名怎么设置安康地seo
  • 北京网站建设付款方式怎么找需要推广的商家
  • 深圳网站建设专业的公司广告营销留电话网站
  • 专业网站建设seo变现培训
  • 如何做公司培训网站湖北网络营销网站
  • 114黄页企业名录在哪里买武汉seo网络优化公司
  • 新疆维吾尔族城乡建设厅网站公司产品推广方案
  • 张家港建网站可以直接打开网站的网页
  • 网站框架优化星巴克seo网络推广
  • 杭州seo网络公司windows优化大师会员兑换码
  • 大坪网站公司茶叶网络推广方案
  • 外贸网站seo招聘江苏seo网络
  • 北京专业建网站的公司广告优化师培训
  • 通信公司网站建设电子邮件营销
  • 网站服务器和空间的区别烟台seo关键词排名
  • 赌城网站怎么做推广普通话文字素材
  • 盐城做网站公司广东省最新疫情
  • 做外贸没有网站可以吗willfast优化工具下载
  • wordpress数据库meta比优化更好的词是
  • 网站推广制作网站如何推广营销
  • 北京网站搜索引擎优化推广关联词有哪些
  • 开原铁岭网站建设加入网络营销公司
  • 杭州网站建设哪家强淄博做网站的公司
  • 如何用ps做网站标识免费网络推广软件有哪些
  • 微信网站开发流程图口碑营销的步骤
  • 图书网站建设实训心得本网站三天换一次域名