3.4_函数_Function_Part_1

来源:互联网 发布:极乐净土网页js代码 编辑:程序博客网 时间:2024/05/16 13:39

函数:程序中可重复使用的程序段

 

def say_hi():

     print(" hi!")

 

#注意 这里 print 之前 要缩进用于 定义函数的结尾

 

say_hi()

say_hi()

 

给一段程程序起一个名字,用这个名字来执行一段程序,反复使用 (调用函数)

 

用关键字 'def' 来定义,identifier(参数)

 

identifier 

 

参数list

 

return statement

 

局部变量 vs 全局变量

 

 

 

 

Code:

 

 

#-*- coding: utf-8 -*-

 

#没有参数和返回的函数

def say_hi():

#     print(" hi!")

#     

# say_hi()

# say_hi()

#    

#    

#有参数,无返回值

def print_sum_two(a, b):

     c = a + b

     print(c)

     

print_sum_two(3, 6)

    

  

def hello_some(str):

#     print("hello " + str + "!")

#         

# hello_some("China")

# hello_some("Python")

 

  

#有参数,有返回值

def repeat_str(str, times):

#     repeated_strs = str * times

#     return repeated_strs

# repeated_strings = repeat_str("Happy Birthday!", 4)

# print(repeated_strings)

    

21

 

#全局变量与局部 变量

# x = 60

def foo(x):

#     print("x is: " + str(x))

#     x = 3

#     print("change local x to " + str(x))

foo(x)

# print('x is still', str(x))

  

  

 

 

 

     

原创粉丝点击