Python Functions Quiz
44 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which of the following is NOT a valid function naming rule?

  • Function names cannot start with a number.
  • Function names should describe the task performed.
  • Function names can contain spaces. (correct)
  • Uppercase and lowercase characters are distinct.
  • What is the purpose of a function definition?

  • To specify what the function does. (correct)
  • To execute the function immediately.
  • To declare the variable types used in the function.
  • To compile the function before use.
  • What is included in the function header?

  • A comment explaining the function.
  • Just the parentheses and colon.
  • Only the function name.
  • Keyword 'def' and function name followed by parentheses and colon. (correct)
  • What happens when a function is called?

    <p>The interpreter jumps to the function, executes its statements, and then returns to the calling program.</p> Signup and view all the answers

    How should a function name be structured for best clarity?

    <p>With descriptive action words, such as a verb.</p> Signup and view all the answers

    What does the special value None indicate when returned from a function?

    <p>An error has occurred</p> Signup and view all the answers

    Which function from the math module returns the smallest integer greater than or equal to x?

    <p>ceil(x)</p> Signup and view all the answers

    Before using the math module in a Python program, which statement is required?

    <p>import math</p> Signup and view all the answers

    What does the function sqrt(x) return?

    <p>The square root of x</p> Signup and view all the answers

    If num2 is zero in the divide(num1, num2) function, what is the expected output?

    <p>None</p> Signup and view all the answers

    What is the scope of a local variable?

    <p>It can only be accessed within the function where it is defined.</p> Signup and view all the answers

    What happens when a function tries to access a local variable that has not been created yet?

    <p>It results in an error.</p> Signup and view all the answers

    How are arguments passed to a function when it is called?

    <p>Arguments are passed by position to the parameters.</p> Signup and view all the answers

    What defines a parameter in a function?

    <p>A variable that receives arguments passed into a function.</p> Signup and view all the answers

    What is the purpose of the penup method in the square function?

    <p>To move the turtle without drawing</p> Signup and view all the answers

    In the circle function, how is the turtle positioned before drawing the circle?

    <p>Using <code>turtle.goto(x, y - radius)</code></p> Signup and view all the answers

    What allows a Python function to accept multiple arguments?

    <p>Using a parameter list separated by commas.</p> Signup and view all the answers

    Which statement about local variables is true?

    <p>Local variables cannot be accessed after the function execution is complete.</p> Signup and view all the answers

    Which line of code begins the filling process for both the square and circle functions?

    <p>turtle.begin_fill()</p> Signup and view all the answers

    What parameters are required to call the square function correctly?

    <p>x, y, width, color</p> Signup and view all the answers

    In the context of function execution, what is the relationship between parameters and arguments?

    <p>Parameters reference the values of arguments during execution.</p> Signup and view all the answers

    In a function that accepts multiple arguments, how does Python match the arguments?

    <p>The first argument goes to the first parameter, the second to the second, etc.</p> Signup and view all the answers

    How many times does the loop execute in the square function to draw the square?

    <p>4 times</p> Signup and view all the answers

    What happens to the argument when a function modifies the parameter value?

    <p>The argument remains unaffected by the changes made to the parameter.</p> Signup and view all the answers

    How are keyword arguments beneficial in function calls?

    <p>They allow for the specification of which parameter the value corresponds to, regardless of order.</p> Signup and view all the answers

    What is a requirement when mixing positional and keyword arguments in a function call?

    <p>Positional arguments must come before keyword arguments.</p> Signup and view all the answers

    What is the term used to describe when changes made in a function do not affect the original variable?

    <p>Pass by value</p> Signup and view all the answers

    In the context of function parameters, which option best describes the use of keyword arguments?

    <p>Their order can differ from the function's parameter list.</p> Signup and view all the answers

    Which statement about keyword arguments is correct?

    <p>They allow for clarity in function calls by explicitly mentioning parameter names.</p> Signup and view all the answers

    When calling a function with both positional and keyword arguments, what type of error occurs if the order is reversed?

    <p>TypeError</p> Signup and view all the answers

    What does the keyword argument format look like when you are calling a function?

    <p>function_name(parameter=value)</p> Signup and view all the answers

    What must be declared inside a function if it needs to assign a value to a global variable?

    <p>global variable</p> Signup and view all the answers

    Why are global variables generally avoided in programming?

    <p>They can lead to confusion about variable values.</p> Signup and view all the answers

    Which of the following statements is true regarding global constants?

    <p>They reference a value that cannot be changed.</p> Signup and view all the answers

    Which action is necessary to utilize a function from a module after importing it?

    <p>Use dot notation to call the function.</p> Signup and view all the answers

    What type of function does not return a value?

    <p>void function</p> Signup and view all the answers

    What is the purpose of a seed value in the context of random number generation?

    <p>To initialize the formula for generating random numbers.</p> Signup and view all the answers

    Which of the following correctly describes the randint function?

    <p>It generates a random integer within a specified range.</p> Signup and view all the answers

    What does the random function return?

    <p>A random float within the range of 0.0 and 1.0.</p> Signup and view all the answers

    What must be included in a return statement in a value-returning function?

    <p>An expression that computes a value</p> Signup and view all the answers

    How can global constants be simulated in Python?

    <p>Create global variables and do not redeclare them within functions.</p> Signup and view all the answers

    What is the main disadvantage of functions using global variables?

    <p>They create dependencies that make function transfer difficult.</p> Signup and view all the answers

    Which statement about the random module is incorrect?

    <p>It cannot be imported into other Python programs.</p> Signup and view all the answers

    What happens when a variable is assigned a value inside a function without declaring it global?

    <p>It is treated as a local variable.</p> Signup and view all the answers

    Study Notes

    Introduction to Functions

    • Function: a group of statements within a program that performs a specific task.
    • Large programs are often broken down into smaller, more manageable tasks, which can be performed by functions.
    • This divide-and-conquer approach simplifies program design, testing, and maintenance.
    • Modularized programs: each task in the program is in its own function.

    Topics

    • Introduction to Functions
    • Defining and Calling a Void Function
    • Designing a Program to Use Functions
    • Local Variables
    • Passing Arguments to Functions
    • Global Variables and Global Constants
    • Turtle Graphics: Modularizing Code with Functions
    • Introduction to Value-Returning Functions
    • Generating Random Numbers
    • Writing Your Own Value-Returning Functions
    • The math Module
    • Storing Functions in Modules

    Void Functions and Value-Returning Functions

    • Void function: performs a task and terminates.
    • Value-returning function: performs a task and returns a value.
    • Value can then be used later in the program.

    Defining and Calling a Void Function

    • Function names must follow Python naming rules.

    • Names cannot use keywords or contain spaces.

    • First character must be a letter or underscore; other characters can be letters, numbers, or underscores.

    • Case-sensitive.

    • Function name should be descriptive of the task it performs.

    • Function definition: specifies a function’s actions.

    • Function header: first line of a function that includes the def keyword, function name, parentheses, and colon.

    • Block: statements that belong together as a group (indentation after the colon).

    • A function definition specifies what a function does but does not execute it.

    • Calling a function to execute it. The interpreter jumps to the function and executes statements contained within the block. It then jumps back to the part of the program that called the function and resumes execution from that point.

    Example of Defining and Calling a Function

    • A function can be called from within another function as well as from the main part of a program.
    • The order of functions matters.

    Defining and Calling a Void Function (cont.)

    • Main function: starts when the program starts and calls other functions as needed; important logic of the program.

    Indentation in Python

    • Each block of code must be indented.
    • Use consistent indentation for readable code.
    • Python interpreters use indentation to determine the structure of the code.

    Designing a Program to Use Functions

    • Flowchart: visual representation of the flow of logic within a function, but it doesn't show the relationship between functions.
    • Hierarchy chart: AKA Structure Chart. Shows relationship between functions. Box for each function; lines illustrate which functions call which functions. Shows only the flow between, but not what happens inside each function.

    Using the pass Keyword

    • Use the pass keyword, when writing a function that has no specific action.

    Local Variables

    • Local variable: a variable assigned a value inside a function.
    • Only accessible inside the function where it was assigned.

    Scope

    • Scope: part of a program where a variable can be accessed (for local variables, it's the function where it's created).
    • Local variables cannot be seen, or used, in any functions that come before their creation.
    • Functions can have local variables with the same name.

    Passing Arguments to Functions

    • Argument: data given to a function when it is called.
    • Parameter: a variable that receives an argument.
    • Arguments are passed to parameters by position.
    • Function can perform calculations by using an argument provided in the code.

    Passing Multiple Arguments

    • Functions can accept multiple arguments.
    • The arguments are passed to corresponding parameters via their position in the parameter list.

    Making Changes to Parameters

    • Changes to parameter values inside a function do not affect the original argument values outside the function.
    • This unidirectional communication method is also known as pass-by-value.

    Keyword Arguments

    • Keyword arguments specify which parameter should receive a given value.
    • The order of keyword arguments does not matter in the function call.

    Mixing Keyword Arguments with Positional Arguments

    • Positional arguments must come before keyword arguments. Otherwise, Python will produce an error.

    Global Variables

    • Global variable: declared outside of all functions
    • Can be accessed by many statements in the program, including from within any function.
    • Can be modified by functions.
    • If a function needs to change a global variable’s value, the statement global var_name must appear at the top of the function.

    Global Constants

    • Constants cannot be changed by functions.
    • Use to give values like pi or other constants in a program.
    • Values are given once, and functions do not change them.

    Introduction to Value-Returning Functions: Generating Random Numbers

    • Value-returning functions: perform a task and return a value.
    • The value is returned to the part of the program that called the function when the function finishes executing.

    Standard Library Functions and the import Statement

    • Standard library: a pre-written set of functions, included with Python, that programmers commonly use.

    Modules

    • Modules: Files that organize the standard library, which helps organize the library functions into files.
    • When calling a function from a module, you must first import the module.
    • Module names normally end with .py. The name should not be the same as a Python keyword.

    Generating Random Numbers

    • Random number generation: the random module (randint(), randrange(), random(), uniform).

    Random Number Seeds

    • Random number seeds are used to initialize random numbers.
    • Different seeds give different series of random numbers.
    • Random seeds are used to adjust the randomness and repeat processes with random functions.

    Writing Your Own Value-Returning Functions

    • Functions that return values are written simply; one or multiple return statements are used.
    • Returns a value specified in the return statement, to the part of the program that called it.
    • Expressions in a return statement may be complex .

    Example: How to Use the return value of a Function

    • Returning values from a function to use in other calculations or to simplify complex calculations.

    How to Use Value-Returning Functions

    • You can use value-returning functions for specific situations involving input, mathematical calculations.
    • Use returned values in variables or as arguments in other functions.

    Using IPO Charts

    • IPO Charts: a tool for describing input, processing, and output in a function.
    • Use columns to lay out the input, processing and output.

    ###Returning Strings

    • Functions can return strings.

    Returning Boolean Values

    • Boolean functions either return True or False.
    • Can use in decision/repetition structures.

    Returning Multiple Values

    • Functions can return multiple values separated by commas

    • Use variables on the left side of the = operator to receive return values.

    Returning None From a Function

    • None: Special value that means there's no return value.
    • Can be returned by functions to indicate an error.

    The math Module

    • Part of the standard library containing helpful mathematical functions, which need to be imported to use.
    • This module includes functions like sqrt(), cos(), sin(), etc. and constants like pi, e.
    • The math module functions are typically called using the dot notation format (module_name.function_name()).

    Storing Functions in Modules

    Conditionally Executing the main Function

    • The special variable __name__ will be set to "main" when the file is run directly, and the name of the module when the file is imported into another program. A simple conditional statement allows you to execute code within main() that you wish only ran when the file is run directly.

    Turtle Graphics

    • Turtle Graphics operations can be stored in functions and called when needed.
    • Functions can be written to perform specific operations on turtles like creating squares, circles and lines.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    CSM2170 Chapter 5 Functions PDF

    Description

    Test your knowledge on Python functions with this quiz. It covers function naming rules, definitions, headers, and behaviors during function calls. Assess your understanding of the math module and common pitfalls when using functions in Python.

    More Like This

    Untitled
    10 questions

    Untitled

    SmoothestChalcedony avatar
    SmoothestChalcedony
    Python Functions
    10 questions

    Python Functions

    StaunchSyntax avatar
    StaunchSyntax
    Python Functions Overview
    29 questions

    Python Functions Overview

    EffortlessArtNouveau1257 avatar
    EffortlessArtNouveau1257
    Use Quizgecko on...
    Browser
    Browser