• 例2-11 阶乘表


    阶乘表

    程序核心——自定义函数

    程序

    #include<stdio.h>
    double fact(int n);//自定义函数声明
    int main()
    {
    	int i,n;
    	double result;
    	
    	printf("Enter n:");
    	scanf("%d",&n);
    	for(i=0;i<=n;i++)
    	{
    		result=fact(i);
    		printf("%d!=%.0f
    ",i,result);
    	}
    	return 0;
     } 
     //定义求n!函数 
     double fact(int n)
     {
     	int i;
     	double product;
     	
     	product=1;
     	for(i=1;i<=n;i++)
     	{
     		product=product*i; 
    	 }
    	return product;
     }
    

    结果

    Enter n:3
    0!=1
    1!=1
    2!=2
    3!=6
    
    --------------------------------
    Process exited after 2.453 seconds with return value 0
    请按任意键继续. . .
    

    分析

    重点:自定义函数在运行时只能返回一个值,如需要返回多个值可用全局变量(影响代码安全,慎用)、数组名与指针(适用返回值类型一致)、结构体指针(类型不一致)

  • 相关阅读:
    快速排序算法
    DirectX9(翻译):介绍
    奇葩的面试题
    新博客
    OpenCV2:幼儿园篇 第八章 视频操作
    编程规范:位运算
    编程规范:allocator
    深浅copy和浅copy
    模块和包
    递归函数
  • 原文地址:https://www.cnblogs.com/5236288kai/p/10534717.html
Copyright © 2020-2023  润新知