当前位置:网站首页>Introduction to C language II. Functions
Introduction to C language II. Functions
2022-07-24 07:32:00 【And move forward with the high wind - & gt;】
Catalog
One . Composition of functions
1. The actual parameter ( Actual parameters ):
2. Formal parameters ( Shape parameter ):
3. Nested calls and chained access to functions
3、 ... and . Function declaration and definition
Four . Recursion and iteration of functions
3. Solve classical problems with function recursion
(2) The problem of frog jumping on the steps
Functions are divided into library functions and user-defined functions . In the process of development, every programmer may use , In order to support portability and improve program efficiency , therefore C A series of similar library functions are provided in the basic library of the language , Convenient for program development .
C Languages have many libraries , Each library contains many library functions .
One . Composition of functions
#include <stdio.h>
// Method 1 cannot complete the task
void Swap1(int x, int y) {
int tmp = 0;
tmp = x;
x = y;
y = tmp;
}
// Mode 2 can be achieved
void Swap2(int *px, int *py) {
int tmp = 0;
tmp = *px;
*px = *py;
*py = tmp;
}1. The actual parameter ( Actual parameters ):
2. Formal parameters ( Shape parameter ):
Two . Function call
1. Value transfer call
2. Address call
3. Nested calls and chained access to functions

3、 ... and . Function declaration and definition
1. Declaration of functions :
2. Definition of function :
Four . Recursion and iteration of functions
1. Recursion of a function
#include<stdio.h>
void print(int n) {
if (n) {
print(n / 10);
printf("%d ", n % 10);
}
}Recursive implementation of the string length function :
int my_strlen(char*str){
if(*str!='\0')
return 1+my_strlen(str+1);
else
return 0;
}2. Iteration of function
// Recursive implementation of functions
int fun1(int n){
if(n==1)return 1;
else
return n*fun1(n-1)
}
// Iterative implementation of function
int fun2(int n){
int ret,i;
for(i=1;i<=n;i++){
ret=ret*i;
}
return ret;
}3. Solve classical problems with function recursion
(1) The hanotta problem

From the analysis , Steps 1 and 3 can be realized recursively .
// Realize the function of finding the total number of times to move the disk
int fun(int n){
if(n==1) return 1;
else
return 2*fun(n-1)+1;
}(2) The problem of frog jumping on the steps
A frog can jump up at a time 1 Stepped steps , You can jump on it 2 level . Ask the frog to jump on one n How many jumps are there in the steps .
List function expressions :( Similar to the Fibonacci sequence )

int fun(int n){
if(n==1)return 1;
if(n==2)return 2;
if(n>2) return fun(n-1)+fun(n-2);
}边栏推荐
- [introduction to C language] zzulioj 1011-1015
- Jay Chou's live broadcast was watched by more than 6.54 million people, with a total interaction volume of 450million, helping Kwai break the record again
- django.db.utils. OperationalError: (2002, “Can‘t connect to local MySQL server through socket ‘/var/r
- Reptile learning - Overview
- csdn,是时候说再见!
- 归纳、概括、演绎
- Advanced part of Nacos
- Oauth2==SSO三种协议。Oauth2四种模式
- 全国职业院校技能大赛网络安全B模块 Windows操作系统渗透测试
- AMD64(x86_64)架构abi文档:上
猜你喜欢

C语言文件操作

项目上线就炸,这谁受得了

My creation anniversary

SPI - send 16 bit and 8-bit data

Harbor2.2 quick check of user role permissions

Oauth2==SSO三种协议。Oauth2四种模式

Single Gmv has increased 100 times. What is the "general rule" behind the rise of popular brands?

Jackson 解析 JSON 详细教程

cloud的版本升级

Requests crawl page source code data
随机推荐
MySQL queries all parents of the current node
给一个字符串 ① 请统计出其中每一个字母出现的次数② 请打印出字母次数最多的那一对
libsvm 使用参数的基础知识笔记(1)
Jenkins 详细部署
Jay Chou's live broadcast was watched by more than 6.54 million people, with a total interaction volume of 450million, helping Kwai break the record again
Oauth2==SSO三种协议。Oauth2四种模式
[line test] Figure finding regular questions
Feature Selective Anchor-Free Module for Single-Shot Object Detection
Network security B module windows operating system penetration test of national vocational college skills competition
[PTA] group programming ladder competition - Summary of exercises L3 (incomplete)
Nacos的高级部分
R language handwritten numeral recognition
Riotboard development board series notes (IX) -- buildreoot porting matchbox
25.消息订阅与发布——PubSub-js
Requests crawl page source code data
Opencascade notes: GP package
Introduction to C language I. branch and loop statements
Pytorch deep learning practice lesson 10 / assignment (basic CNN)
Log in to the server using the fortress machine (springboard machine)
Bookkeeping app: xiaoha bookkeeping 1 - production of welcome page