You are on page 1of 5

Add the heading “1-9 Writing Simple Functions” to your learning journal and

answer the following questions:

1. Briefly explain the four main benefits of designing a program with functions.

It makes the code simpler and easier to understand,as well it helps


to recycle and reuse the code instead of repeating specific lines over
and over.In addition,finding errors will be easier and easier to
isolate instead of checking hundreds of lines of code.Lastly,it helps
big teams work together simultaneously without having to worry
about each other’s code and how their software works in details.

2. Name and describe the two parts of a function definition.

First,the function header which is made of the def keyword ,name


of the function ,brackets to place parameters if needed ,and a colon
looks like this “ def name():”.It marks the beginning of the function

Secondly,the block is a group of indented statements which has the


functions’ code and functionality.They must all be equally indented
using only spaces or tabs

3. Why must we always indent the code in a function, is it a Python syntax


requirement or just considered good style?
It is a requirement so the interpreter knows when a specific block
starts and ends.However,having indented lines make it easier to
follow and read.

4. What happens to the flow of execution when a function is called? What happens
when the end of the function’s block is reached?

When a function is called the interpreter goes to the function to


execute its lines and when done it goes back to the line after the
function was called.

5. Where is a local variable defined, and what is its scope?

A local variable is a variable defined in and used by a specific


function that can’t be accessed outside as the variable’s scope is
restricted to this function .

A scope of a variable refers to the parts of the code that the variable
can be accessed from without errors including a function,module or
class
6. Is it permissible to use the same local variable name in two different functions?
Explain why or why not. Write an example program.

Yes,because the memory will erase the variable value after the
function is fully executed .So,having two local variables for two
different functions will not interfere with each other or change their
values.This due their different scopes so each function will have its
own version of the local variable.

def display_name():
name = "Johnny" #local variable 1
print(name)

def display_name2():
name = "Kozman" #local variable 2
print(name)

display_name()
display_name2()#there is no overlapping each function used
the name variable defined inside it
2 - Write a function called display_face() that prints a face, using text
characters, hopefully, better looking than this one:

/////

| o o |

(| ^ |)

| [_] |

=====

Add mainline logic (i.e., a main() function) to your program to demonstrate how to call
your function.

def main():
display_hair()
display_eyes()
display_nose()
display_smile()
display_end()

def display_hair():
print(" /c/c/c/c/ ")

def display_eyes():
print(" |-[o]-[o]-|")
def display_smile():
print(" | \-\_/-/ |")
def display_nose():
print("(| **** |)")
def display_end():
print("===========")

main()

You might also like