Python Functions Quiz
44 Questions
1 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. (B)</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. (C)</p> Signup and view all the answers

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

<p>An error has occurred (A)</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) (B)</p> Signup and view all the answers

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

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

What does the function sqrt(x) return?

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

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

<p>None (A)</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. (C)</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. (C)</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. (A)</p> Signup and view all the answers

What defines a parameter in a function?

<p>A variable that receives arguments passed into a function. (C)</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 (A)</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> (D)</p> Signup and view all the answers

What allows a Python function to accept multiple arguments?

<p>Using a parameter list separated by commas. (D)</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. (B)</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() (B)</p> Signup and view all the answers

What parameters are required to call the square function correctly?

<p>x, y, width, color (B)</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. (D)</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. (C)</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 (A)</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. (C)</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. (B)</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. (D)</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 (D)</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. (B)</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. (B)</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 (B)</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) (B)</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 (C)</p> Signup and view all the answers

Why are global variables generally avoided in programming?

<p>They can lead to confusion about variable values. (A)</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. (A)</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. (A)</p> Signup and view all the answers

What type of function does not return a value?

<p>void function (A)</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. (C)</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. (B)</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. (D)</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 (B)</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. (A)</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. (C)</p> Signup and view all the answers

Which statement about the random module is incorrect?

<p>It cannot be imported into other Python programs. (C)</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. (C)</p> Signup and view all the answers

Flashcards

Function Naming Rules

Function names must follow specific rules, avoiding keywords, spaces, and starting with symbols unless an underscore.

Function Definition

Specifies what a function does. It doesn't automatically run.

Function Call

Actively running a function's code to perform its task.

Function Header

The first line of a function definition, that includes the def keyword, function name, parentheses, and colon.

Signup and view all the flashcards

Function Block

The set of statements a function performs.

Signup and view all the flashcards

Passing Multiple Arguments

Functions can accept more than one input value.

Signup and view all the flashcards

Pass by value

Changes to a function parameter don't affect the original argument.

Signup and view all the flashcards

Keyword Arguments

Arguments that specify parameters directly by name.

Signup and view all the flashcards

Positional Arguments

Arguments passed based on their position in the function call.

Signup and view all the flashcards

Mixing Arguments

Combining positional and keyword arguments in function calls, with positional first.

Signup and view all the flashcards

Function Parameter

Represents a variable that receives input values within a function.

Signup and view all the flashcards

Argument

Input value given to a function when it's called.

Signup and view all the flashcards

Unidirectional Communication

Data passed from a calling function to a called function, not the other way around.

Signup and view all the flashcards

Local Variable

A variable declared inside a function. It can only be used within that specific function.

Signup and view all the flashcards

Scope of a variable

The part of a program where a variable can be accessed.

Signup and view all the flashcards

Parameter

A variable that receives an argument passed to a function.

Signup and view all the flashcards

Passing Arguments

Sending data to a function through arguments.

Signup and view all the flashcards

Multiple Arguments

Passing more than one argument to a function.

Signup and view all the flashcards

Parameter List

A list of parameters in the function definition, separated by commas.

Signup and view all the flashcards

What does 'None' mean in Python?

It represents the absence of a value. It indicates that a function didn't return anything meaningful.

Signup and view all the flashcards

When is 'None' useful in functions?

It's helpful when you need a function to signal an error or an unsuccessful operation, letting the calling code know something went wrong.

Signup and view all the flashcards

What's the math module?

A built-in collection of functions designed for mathematical operations.

Signup and view all the flashcards

How to use math functions?

First, import the 'math' module using 'import math'. Then, use the function name followed by parentheses (e.g., 'math.sqrt(9)' for square root of 9).

Signup and view all the flashcards

What does 'acos(x)' do?

It calculates the inverse cosine (arccosine) of 'x', giving the angle in radians.

Signup and view all the flashcards

What does the square() function do?

The square() function draws a square using the turtle graphics library. It takes four arguments:

  • x: The x-coordinate of the top-left corner of the square.
  • y: The y-coordinate of the top-left corner of the square.
  • width: The side length of the square.
  • color: The color to fill the square.
Signup and view all the flashcards

What is the purpose of the turtle.goto(x, y) command?

The turtle.goto(x, y) command moves the turtle to a specific location on the screen. The x and y arguments specify the coordinates of the desired location. This command is used to position the turtle before drawing shapes.

Signup and view all the flashcards

What is the benefit of using functions for drawing shapes?

Using functions to draw shapes makes the code more organized and reusable. Instead of writing the drawing code multiple times for each shape, you can define a function once and call it whenever you need to draw that shape.

Signup and view all the flashcards

What is the purpose of the turtle.begin_fill() and turtle.end_fill() commands?

The turtle.begin_fill() command starts filling a shape with the currently selected fill color. The turtle.end_fill() command ends the filling process, creating a filled shape.

Signup and view all the flashcards

What does the circle() function do?

The circle() function draws a circle using the turtle graphics library. It takes four arguments:

  • x: The x-coordinate of the center of the circle.
  • y: The y-coordinate of the center of the circle.
  • radius: The radius of the circle.
  • color: The color to fill the circle.
Signup and view all the flashcards

Global Variable Declaration (Inside Function)

To modify a global variable from inside a function, you must explicitly declare it as 'global' using the 'global' keyword.

Signup and view all the flashcards

Global Constant

A global variable whose value cannot be changed after initialization.

Signup and view all the flashcards

Void Function

Performs a specific task but does not return a value to the part of the program that called it.

Signup and view all the flashcards

Value-Returning Function

Similar to a void function but returns a value to the caller. The returned value can be used in the calling part of the program.

Signup and view all the flashcards

Standard Library

A collection of pre-written functions that come with Python and provide common programming tasks.

Signup and view all the flashcards

Module

A file that stores functions of the standard library, helping to organize them.

Signup and view all the flashcards

Import Statement

Used to bring in modules and their functions into your program.

Signup and view all the flashcards

Dot Notation

Used to call functions from a module. It uses the format module_name.function_name().

Signup and view all the flashcards

Random Module

A module in Python's standard library that provides functions for generating random numbers.

Signup and view all the flashcards

randint Function

A function in the random module that generates a random integer within a specified range.

Signup and view all the flashcards

Random Function

A function in the random module that generates a random floating-point number between 0.0 and 1.0.

Signup and view all the flashcards

Seed Value

A value used to initialize the formula that generates random numbers. It can be used to produce a consistent sequence when the same seed is used.

Signup and view all the flashcards

Return Statement

Used in a function to send a value back to the part of the program that called the function.

Signup and view all the flashcards

Value-Returning Function Usage

Can be helpful for simplifying complex calculations, getting user input, or performing repeated tasks.

Signup and view all the flashcards

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 Overview
29 questions

Python Functions Overview

EffortlessArtNouveau1257 avatar
EffortlessArtNouveau1257
Use Quizgecko on...
Browser
Browser