stoneskin

learning coding with Scratch , Python and Java

07 Functions

7.1 Python Functions

A function is a block of organized, reusable code that is used to perform a single, related action.

function

7.1.1 Define a function

User def keyword to define a simple function

def hello_world():
    print("Hello, world!")
# call the function Hello_world
hello_world()

7.1.2 Function with parameters

function with parameters

7.1.3 Function return with value

function with return

7.2 Practice function

7.2.1 Write a function return a value

Please write a function with below requirement

hint:

result=0
for x in range(5):
    v=x+1
    result=result+v

7.2.2 Function call another functions

7.2.3 Recursion function

Do not use for loop, and make the function to get result same as 7.2.1

#define recursion function addmore
def addMore(n,max):
    if(n==max):
        result= n
    else:
        result=n+addMore(n+1,max)
    
    print ("n=",str(n)," result=",str(result))
    return result

#end function addMore
r=addMore(1,100)
print(r)