6 Functions and Procedures

Sub Routines

Sub routines are blocks of code that:

  • can be reused
  • break down a program into manageable chunks

When the program is run:

  • the arguments (3,4) are passed into the procedure add parameters
  • c= 3+4
  • the output “7” is output

Sub Routines

1
2
3
4
5
def add(a,b):
    c=a+b
    print(c)

add(3,4)
1
2
3
4
5
6
7
8
9
10
11
12
13
def add(a,b):
    c=a+b
    print(c)
def take(a,b):
    c=a-b
    print(c)
def multiply(a,b):
    c=a*b
    print(c)

add(3,4)
take(3,4)
multiply(3,4)

Functions and Procedures

This is a function because it returns a value

1
2
3
4
5
def add(a,b):
    c=a+b
    return c

print(add(3,4))

This is a procedure because it carries out instructions

1
2
3
4
def add(a,b):
    c=a+b
    print(c)
add(3,4)