Docsity

Funzioni in Programmazione: Guida Dettagliata, Schemi e mappe concettuali di Elementi di Informatica

Anteprima parziale del testo

Scarica Funzioni in Programmazione: Guida Dettagliata e più Schemi e mappe concettuali in PDF di Elementi di Informatica solo su Docsity!

Capitolo 5: Funzioni

Chapter Goals

  • To be able to implement functions
  • To become familiar with the concept of parameter passing
  • To develop strategies for decomposing complex tasks into simpler ones
  • To be able to determine the scope of a variable
  • To learn how to think recursively In this chapter, you will learn how to design and implement your own functions Using the process of stepwise refinement, you will be able to break up complex tasks into sets of cooperating functions

Functions as Black Boxes

SECTION 5.

Functions as Black Boxes

  • A function is a sequence of instructions with a name
  • For example, the round function, which was introduced in Chapter 2, contains instructions to round a floating-point value to a specified number of decimal places

Calling Functions (2)

  • The round function returns its result back to where the function was called and your program resumes execution

Function Arguments

  • When another function calls the round function, it provides “inputs”, such as the values 6.8275 and 2 in the call round(6.8275, 2)
  • These values are called the arguments of the function call
    • Note that they are not necessarily inputs provided by a human user
    • They are the values for which we want the function to compute a result

Function Return Values

  • The “output” that the round function computes is called the return value
  • Functions return only one value
  • The return value of a function is returned to the point in your program where the function was called price = round(6.8275, 2)
  • When the round function returns its result, the return value is stored in the variable ‘price’ statement)

Function Return Values (2)

  • Do not confuse returning a value with producing program output which is produced when using a print() statement

The round Function as a Black Box

  • You pass the round function its necessary arguments (6.8275 & 2) and it produces its result (6.83)

The round Function as a Black Box

  • You may wonder how the round function performs its job
  • As a user of the function, you don’t need to know how the function is implemented
  • You just need to know the specification of the function:
    • If you provide arguments x and n, the function returns x rounded to n decimal digits

Implementing and Testing Functions

SECTION 5.

Implementing and Testing Functions

  • A function to calculate the volume of a cube
    • What does it need to do its job?
    • What does it answer with?
  • When writing (‘defining’) this function
    • Pick a name for the function (cubeVolume)
    • Declare a variable for each incoming argument (sideLength) (called parameter variables)
    • Put all this information together along with the def keyword to form the first line of the function’s definition: def cubeVolume(sideLength): This line is called the header of the function

Calling/Testing the Cube Function

def cubeVolume(sideLength) : volume = sideLength ** 3 return volume result1 = cubeVolume(2) result2 = cubeVolume(10) print("A cube with side length 2 has volume", result1) print("A cube with side length 10 has volume", result2) Implementing the function (function definition) Calling/testing the function

Syntax: Function Definition