Programming Sheet 04: Functions Solution



State whether true or false:-

  1. Programmers rarely define their own functions.

  1. A function may only be called at one place in a program.

  1. Information can be passed into a function through parameters.

  1. Every Python function returns some value.

  1. In Python, some parameters are passed by reference.

  2. In Python, a function can return only one value.

  1. Python functions can never modify a parameter.

  1. One reason to use functions is to reduce code duplication.

  1. Variables defined in a function are local to that function.

  1. It’s a bad idea to define new functions if it makes a program longer.

Multiple Choice

  1. The part of a program that uses a function is called the

    1. user

    2. caller

    3. callee

    4. statement

  2. A Python function definition begins with

    1. def

    1. define

    2. function

    3. defun

  1. A function can send output back to the program with a(n)

    1. return

    2. print

    3. assignment

    4. SASE

  2. Formal and actual parameters are matched up by

    1. Name

    2. Position

    3. ID

    4. interests

  3. Which of the following is not a step in the function-calling process?

    1. The calling program suspends.

    2. The formal parameters are assigned the value of the actual parameters.

    3. The body of the function executes.

    4. Control returns to the point just before the function was called.

  1. In Python, actual parameters are passed to functions

    1. by value

    2. by reference

    3. at random

    4. by networking

  2. Which of the following is not a reason to use functions?

    1. to reduce code duplication

    2. to make a program more modular

    1. to make a program more self-documenting

    2. to demonstrate intellectual superiority

  1. If a function returns a value, it should generally be called from

    1. an expression

    2. a different program

    3. main

    4. a cell phone

  2. A function with no return statement returns

    1. nothing

    2. its parameters

    3. its variables

    4. None

  3. A function can modify the value of an actual parameter only if it’s

    1. Mutable

    1. a list

    2. passed by reference

    3. a variable


  1. In your own words, describe the two motivations for defining functions in your programs.

  1. We have been thinking about computer programs as sequences of instructions where the computer methodically executes one instruction and then moves on to the next one. Do programs that contain functions fit this model? Explain your answer.

  2. Parameters are an important concept in defining functions.

    1. What is the purpose of parameters?

    1. In what ways are parameters similar to and different from ordinary variables?

  1. Functions can be thought of as miniature (sub) programs inside other programs. Like any other program, we can think of functions as having input and output to communicate with the main program.

    1. How does a program provide “input” to one of its functions?

    2. How does a function provide “output” to the program?


Programming Exercises:

1- Write definitions for the following two functions:

a) sumN (n) returns the sum of the first n natural numbers.

b) sumNCubes (n) returns the sum of the cubes of the first n natural numbers. Then use these functions in a program that prompts a user for an n and prints out the sum of the first n natural numbers and the sum of the cubes of the first n natural numbers.

2- Write a function to compute the factorial of a given positive integer.

3- Write a function to compute the nth Fibonacci number.

4- Write a function to compute the following summation:-

1 1 1 1 1


Hint: the value of this summation is the well-known constant π. Show how to handle the infinity number of terms.


error: Content is protected !!