Draw a flowchart that shows how a function named 'greet' works. The function takes a name as input and prints 'Hello, [name]!'. Then show how the main program calls this function with the name 'Alice'.
Functions (reusable code blocks) in Intro to Computing - Draw & Build Visually
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Draw This - beginner
Grading Criteria
Solution
+-------------------+ +-------------------------+
| Start | | Start Function |
+-------------------+ +-------------------------+
| |
v v
+-------------------+ +-------------------------+
| Call greet('Alice')|--------->| Receive parameter name |
+-------------------+ +-------------------------+
| |
| v
| +-------------------------+
| | Print "Hello, [name]!" |
| +-------------------------+
| |
| +-------------------------+
| | End Function |
| +-------------------------+
v |
+-------------------+ |
| End |<-----------------+
+-------------------+This flowchart shows two parts: the main program and the function named 'greet'.
1. The main program starts and calls the function 'greet' with the input 'Alice'.
2. The function starts and receives the input parameter called 'name'.
3. Inside the function, it prints the message "Hello, Alice!" by inserting the input name into the greeting.
4. The function ends and returns control back to the main program.
5. The main program then ends.
This shows how functions let us reuse code by defining a block that can be called with different inputs.
Variations - 2 Challenges
[intermediate] Draw a flowchart for a function named 'add' that takes two numbers as input and returns their sum. Show the main program calling 'add' with 5 and 3, then printing the result.
[advanced] Draw a flowchart for a function named 'is_even' that takes a number as input and returns 'Yes' if the number is even, or 'No' if it is odd. Show the main program calling 'is_even' with 10 and printing the result.
Practice
1. What is the main purpose of a function in programming?
easy
Solution
Step 1: Understand what a function does
A function is a reusable block of code designed to perform a specific task.Step 2: Identify the main purpose
Functions help avoid repeating the same code by allowing reuse whenever needed.Final Answer:
To reuse a block of code multiple times -> Option BQuick Check:
Function purpose = reuse code [OK]
Hint: Functions help reuse code blocks easily [OK]
Common Mistakes:
- Confusing functions with variables
- Thinking functions store data permanently
- Believing functions only display output
2. Which of the following is the correct way to define a function named
greet that takes no inputs?easy
Solution
Step 1: Recognize Python function syntax
In Python, functions are defined using the keyworddef, followed by the function name and parentheses with parameters (empty if none), ending with a colon.Step 2: Check each option
def greet():matches Python syntax.function greet() {}andfunction greet:use JavaScript style or incorrect syntax.def greet()misses the colon.Final Answer:
def greet(): -> Option DQuick Check:
Python function = def name(): [OK]
Hint: Python functions start with def and end with colon [OK]
Common Mistakes:
- Omitting the colon at the end
- Using JavaScript syntax in Python
- Leaving out parentheses
3. What will be the output of this code?
def add(x, y):
return x + y
result = add(3, 4)
print(result)medium
Solution
Step 1: Understand the function call
The functionaddtakes two inputsxandyand returns their sum.Step 2: Calculate the return value
Callingadd(3, 4)returns 3 + 4 = 7, which is stored inresult. Printingresultoutputs 7.Final Answer:
7 -> Option CQuick Check:
3 + 4 = 7 [OK]
Hint: Add numbers inside function returns their sum [OK]
Common Mistakes:
- Thinking it concatenates numbers as strings
- Expecting a TypeError due to missing return
- Assuming print shows None
4. Identify the error in this function definition:
def multiply(a, b)
return a * bmedium
Solution
Step 1: Check function header syntax
In Python, the function header must end with a colon (:). Here, it is missing afterdef multiply(a, b).Step 2: Verify other parts
The return statement and function name are correct. Parameters use parentheses, not square brackets.Final Answer:
Missing colon after function header -> Option AQuick Check:
Function header ends with : [OK]
Hint: Function headers always end with a colon in Python [OK]
Common Mistakes:
- Forgetting the colon at the end
- Using square brackets for parameters
- Misnaming the function
5. You want to create a function that returns the square of a number only if the number is positive; otherwise, it returns zero. Which function correctly implements this?
hard
Solution
Step 1: Understand the condition
The function should return the square ofnonly ifnis positive (greater than 0). Otherwise, it returns 0.Step 2: Check each option
def square_if_positive(n): if n > 0: return n * n else: return 0 correctly checksn > 0and returnsn * n, else 0. def square_if_positive(n): if n >= 0: return n ** 2 else: return n includes zero as positive and returnsnif negative, which is incorrect. def square_if_positive(n): return n * n if n > 0 else None returns None instead of 0 whennis not positive. def square_if_positive(n): if n < 0: return n * n else: return 0 squares negative numbers and returns 0 otherwise, which is opposite.Final Answer:
def square_if_positive(n): if n > 0: return n * n else: return 0 -> Option AQuick Check:
Positive n squared, else zero [OK]
Hint: Check condition n > 0, return square else zero [OK]
Common Mistakes:
- Including zero as positive
- Returning None instead of zero
- Reversing the condition logic
