第4章 函数与程序结构

4-1 函数基础

‘tips:Google命名规范’

#include <stdio.h>

double f(double x) {
    //在内面的参数叫形式参数
    return x * x + x + 1;
}

double g(double x, double y,double z){
    return x * x + y * y + z * z;
}

int main(){
    // 这是传的 2.0 就是实际参数
    double result_f = f(2.0);
    double result_g = g(1.0, 3.0, 4.0);
    printf("result_f of f: %f\n", result_f);
    printf("result_f of g: %g\n", result_g);
    return 0;
}

用谷歌的命名规范

Clion 快捷键:Ctrl + Alt + S 或 File -> Settings

Editor -> Code Style -> C/C++

  1. Set from -> Google

  2. -> Naming Convertion -> Global Funtion

  3. apply

在这里插入图片描述
如果不提示,需要手动打开(鼠标移入到小对号上)

在这里插入图片描述
需要把【Inconsistent Naming】选上
在这里插入图片描述

提示后代码为

#include <stdio.h>

double F(double x) {
    //在内面的参数叫形式参数
    return x * x + x + 1;
}

double G(double x, double y, double z){
    return x * x + y * y + z * z;
}

int main(){
    // 这是传的 2.0 就是实际参数
    double result_f = F(2.0);
    double result_g = G(1.0, 3.0, 4.0);
    printf("result_f of f: %F\n", result_f);
    printf("result_f of g: %G\n", result_g);
    return 0;
}

4-2 函数的原型

‘【空参数列表与 C++的区别】’

#include <stdio.h>

void EmptyParmList(void);

/*
 * 1、函数名
 * 2、函数返回值类型,如里没写,默认为int
 * 3、函数参数列表,参数类型,和参数的顺序,参数形参名不重要
 */
int Add(int, int);

int main(void){
  puts("");
  EmptyParmList();

  int result = Add(1, 2);
  printf("result of add: %d\n", result);
  return 0;
  
}

4-3 变量的类型和作用域

‘【Compiler Explorer查看汇编指令】’

1、自动变量auto

​ 1、函数、块作用域,随着函数和块退出而销毁
​ 2、没有默认初值

2、静态变量 static

​ 1.作用域全局,内存不会因为函数退出而销毁
​ 2.int 初始默认为0

3、寄存器变量 register

#include <stdio.h>
//file scope
int global_val = 1;

void LocalStaticVar(void){
  //静态变量
  // 1.作用域全局,内存不会因为函数退出而销毁
  // 2.int 初始默认为0
  static int static_var;

  /*
   * 自动变量
   * 1、函数、块作用域,随着函数和块退出而销毁
   * 2、没有默认初值
   */
  int non_static_var;

  printf("static var: %d\n", static_var++);
  printf("non static var: %d\n", non_static_var++);
}

double  Add(double a,double b);

void CleanMemory(){
  int eraser = -1;
}

// proto scope
// double Sort(int size,int array[size]);

void PassByMemory(int parameter){
  printf("%d\n", parameter);
}
int main(void){
  // 自动变量
  auto int value = 0;

  {// block scope
    auto int a = 0;
    printf("%d\n", a);
  }

  //printf("%d\n", a);

  if(value > 0){
    int is_value_equals_0 = 0, b = is_value_equals_0;
  }
    //is_value_equals_0 success
  else{
    
  }
  return 0;
}

4-4 函数的变长参数

对比Java的变长参数

#include <stdio.h>
#include <stdarg.h>
void HandleVarargs(int arg_count, ...) {
    
  //1、定义va_list 用于获取我们变长参数
  va_list args;

  //2、开始遍历
  va_start(args,arg_count);
  for (int i = 0; i < arg_count; ++i) {
    //3、取出对应参数,(va_list,type)
    int arg = va_arg(args, int);
    printf("d%:d%\n", i, arg);
    vsw
  }

  //4、遍历结束
  va_end(args);
}

int main(void){
  HandleVarargs(4, 1, 2, 3, 4);
  return 0;
}

4-5 函数的递归

C语言实现斐波那契数列

#include <stdio.h>
// 递归阶乘
unsigned int Factoriat(unsigned int n) {
  if (n == 0) {
    // f(0) = 1
    return 1;
  } else {
    // f(n) = nf(n - 1)
    return n * Factoriat(n - 1);
  }
}
// 循环阶乘
unsigned int FactorialByIteration(unsigned int n) {
  unsigned int result = 1;
  for (unsigned int i = n; i > 0; --i) {
    result *= i;
  }
  return result;
}

// 递归斐波那契数列
unsigned int Fibonacci(unsigned int n) {
  if (n == 1 || n == 0) {
    // f(0) = 0, f(1) = 1
    return n;
  } else {
    // f(n) = f(n - 1) + f(n - 2)
    return Fibonacci(n - 1) + Fibonacci(n - 2);
  }
}
// 循环斐波那契数列
unsigned int FibonacciByIterration(unsigned int n){
  if (n == 1 || n == 0) {
    return n;
  }
  unsigned int last = 0;
  unsigned int current = 1;
  for (int i = 0; i <= n - 2; ++i) {
    unsigned  int temp = current;
    current += last;
    last = temp;
  }
  return current;
}

int main(void){
  printf("3!: %d\n", Factoriat(3));
  printf("5!: %d\n", Factoriat(5));
  printf("10!: %d\n", FactorialByIteration(10));

  printf("Fibonacci(3): %d\n", Fibonacci(3));
  printf("Fibonacci(5): %d\n", Fibonacci(5));
  printf("Fibonacci(10): %d\n", FibonacciByIterration(10));
  return 0;
}
#include <stdio.h>
/**
 * Tower of Hanoi 汉诺塔 
 * @param n The count of plates
 * @param src The source of the plates to move from
 * @param dest The destination of the plates to move to
 * @param tmp The temporary place to use
 */
void Move(int n, char src, char dest, char tmp) {
  if(n == 0)return;
  else if(n == 1) printf("%c --> %c\n", src, dest);
  else{
    Move(n - 1, src, tmp, dest);
    Move(1, src, dest, tmp);
    Move(n - 1, tmp, dest, src);

  }
}

int main(void){
  Move(10, 'A', 'B', 'C');
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶喜儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值