zoukankan      html  css  js  c++  java
  • 函数介绍

    函数介绍

    <1>什么是函数

    请看如下代码:

    
    print "                            _ooOoo_  "
        print "                           o8888888o  "
        print "                           88  .  88  "
        print "                           (| -_- |)  "
        print "                            O\ = /O  "
        print "                        ____/`---'\____  "
        print "                      .   ' \| |// `.  "
        print "                       / \||| : |||// \  "
        print "                     / _||||| -:- |||||- \  "
        print "                       | | \\\ - /// | |  "
        print "                     | \_| ''\---/'' | |  "
        print "                      \ .-\__ `-` ___/-. /  "
        print "                   ___`. .' /--.--\ `. . __  "
        print "                ."" '< `.___\_<|>_/___.' >'"".  "
        print "               | | : `- \`.;`\ _ /`;.`/ - ` : | |  "
        print "                 \ \ `-. \_ __\ /__ _/ .-` / /  "
        print "         ======`-.____`-.___\_____/___.-`____.-'======  "
        print "                            `=---='  "
        print "  "
        print "         .............................................  "
        print "                  佛祖镇楼                  BUG辟易  "
        print "          佛曰:  "
        print "                  写字楼里写字间,写字间里程序员;  "
        print "                  程序人员写程序,又拿程序换酒钱。  "
        print "                  酒醒只在网上坐,酒醉还来网下眠;  "
        print "                  酒醉酒醒日复日,网上网下年复年。  "
        print "                  但愿老死电脑间,不愿鞠躬老板前;  "
        print "                  奔驰宝马贵者趣,公交自行程序员。  "
        print "                  别人笑我忒疯癫,我笑自己命太贱;  "
        print "                  不见满街漂亮妹,哪个归得程序员?"
    
    

    运行后的现象:

    image

    想一想:

    如果一个程序在不同的地方需要输出“佛祖镇楼”,程序应该怎样设计?

        if 条件1:
            输出‘佛祖镇楼’
    
        ...(省略)...    if 条件2:
            输出‘佛祖镇楼’
    
        ...(省略)...
    

    如果需要输出多次,是否意味着要编写这块代码多次呢?

    小总结:

    • 如果在开发程序时,需要某块代码多次,但是为了提高编写的效率以及代码的重用,所以把具有独立功能的代码块组织为一个小模块,这就是函数

    函数定义和调用

    <1>定义函数

    定义函数的格式如下:

    
       def 函数名():
            代码
    
    

    demo:

    定义一个函数,能够完成打印信息的功能

    def printInfo():
        print '------------------------------------'
        print '         人生苦短,我用Python'
        print '------------------------------------'
    

    <2>调用函数

    定义了函数之后,就相当于有了一个具有某些功能的代码,想要让这些代码能够执行,需要调用它

    调用函数很简单的,通过 **函数名() **即可完成调用

    demo:

    定义函数的格式如下:

    
    
     # 定义完函数后,函数是不会自动执行的,需要调用它才可以
        printInfo()
    
    </pre>
    
    ## <3>练一练
    
    要求:定义一个函数,能够输出自己的姓名和年龄,并且调用这个函数让它执行
    
    *   使用def定义函数
    
    *   编写完函数之后,通过 **函数名() **进行调用
    
    # 函数的文档说明
    
    
    
      >>> def test(a,b):...   
    

    "用来完成对2个数求和"...

    print("%d"%(a+b))... >>> >>> test(11,22)33
    
    

    如果执行,以下代码

          >>> help(test)
    
    

    能够看到test函数的相关说明

    
    Help on function test in module __main__:
    
    test(a, b)
        用来完成对2个数求和
    (END)
    
    

    原文链接:做最专业最懂你的编程微刊技术分享平台,提供你最需要的开发学习资源。 我们专注于编程开发技术的学习与交流,我们坚持,每天进步一小步,人生进步一大步!关注【编程微刊】,与我们一起学习进步。https://www.jianshu.com/u/05f416aefbe1

  • 相关阅读:
    Leetcode Spiral Matrix
    Leetcode Sqrt(x)
    Leetcode Pow(x,n)
    Leetcode Rotate Image
    Leetcode Multiply Strings
    Leetcode Length of Last Word
    Topcoder SRM 626 DIV2 SumOfPower
    Topcoder SRM 626 DIV2 FixedDiceGameDiv2
    Leetcode Largest Rectangle in Histogram
    Leetcode Set Matrix Zeroes
  • 原文地址:https://www.cnblogs.com/wangting888/p/9701841.html
Copyright © 2011-2022 走看看