Starting from:
$30

$24

Programming Sheet 04: Functions Solution




State whether true or false:-

Programmers rarely define their own functions.



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



Information can be passed into a function through parameters.



Every Python function returns some value.



In Python, some parameters are passed by reference.
In Python, a function can return only one value.



Python functions can never modify a parameter.



One reason to use functions is to reduce code duplication.



Variables defined in a function are local to that function.



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






Multiple Choice

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

user
caller
callee
statement
A Python function definition begins with

def



define
function
defun
A function can send output back to the program with a(n)

return
print
assignment
SASE
Formal and actual parameters are matched up by

Name
Position
ID
interests
Which of the following is not a step in the function-calling process?



The calling program suspends.
The formal parameters are assigned the value of the actual parameters.
The body of the function executes.
Control returns to the point just before the function was called.
In Python, actual parameters are passed to functions

by value
by reference
at random
by networking
Which of the following is not a reason to use functions?

to reduce code duplication
to make a program more modular



to make a program more self-documenting
to demonstrate intellectual superiority
If a function returns a value, it should generally be called from

an expression
a different program
main
a cell phone
A function with no return statement returns

nothing
its parameters
its variables
None
A function can modify the value of an actual parameter only if it's

Mutable



a list
passed by reference
a variable



Discussion:




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



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.
Parameters are an important concept in defining functions.

What is the purpose of parameters?



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



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.



How does a program provide "input" to one of its functions?
How does a function provide "output" to the program?



5.

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

4(1−3+5−7+9−11……)




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




----------------------------------------------------------------------------------------------------------

More products