Programming Techniques DT143G: Functions Lecture 6
13 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

What happens to modifications made to arguments passed into a function?

  • Modifications are only seen within the function. (correct)
  • Arguments are passed by reference.
  • They can alter global variables directly.
  • They are reflected outside the function.
  • What is a reason to avoid using global variables?

  • They increase data accessibility in function scopes.
  • They do not exist outside the main function.
  • They can lead to conflicts with local variable names. (correct)
  • Global variables are always passed to functions.
  • What must be done before using a function in programming?

  • Define a return type for the function.
  • Include a return statement within the function.
  • Declare the function prototype. (correct)
  • Initialize all the global variables.
  • Which of the following statements is true regarding the function power in the provided example?

    <p>It returns a long value.</p> Signup and view all the answers

    What is a correct characteristic of a function that returns void?

    <p>It does not return a value to the caller.</p> Signup and view all the answers

    What is one of the primary reasons for using functions in programming?

    <p>To reuse code at different locations</p> Signup and view all the answers

    Which statement best describes the idea of a 'black box' in relation to functions?

    <p>You define the inputs and outputs without needing to know the internal workings.</p> Signup and view all the answers

    What does the function in the provided example return?

    <p>The combined powers of x and y along with their product</p> Signup and view all the answers

    What type of values can a function in C return?

    <p>A single value of a specified return type</p> Signup and view all the answers

    In the example provided, what does the 'pow' function do?

    <p>Raises a number to the specified power</p> Signup and view all the answers

    Which of the following is true about side effects in functions?

    <p>They can change values outside of the function's scope.</p> Signup and view all the answers

    What does the 'parameter_list' in a function definition signify?

    <p>The inputs that the function can accept</p> Signup and view all the answers

    What could happen if a function causes implicit modifications?

    <p>The code becomes less readable and harder to maintain.</p> Signup and view all the answers

    Study Notes

    Lecture 6: Functions

    • The lecture is about functions in programming language, specifically focusing on programmeringsteknik DT143G.
    • Functions are used to reuse code, increase readability, and manage complex code in a modular manner.
    • Functions are like "black-boxes," where you define the input and the function does the processing, returning the desired output.

    Today's Contents

    • Functions
    • Parameters
    • Arguments

    Why Functions?

    • Functions are used to reuse code for different parts of a program.
    • They improve code readability.
    • Functions make it easier to manage bigger pieces of code.

    Functions Overview

    • Functions are key features of programming languages (except very basic ones).
    • They are similar to mathematical functions.
    • The format starts with return_type func_name(parameter_list) followed by the function body enclosed in curly braces.

    Input, Output & Side Effects

    • Input: A function can have zero or more inputs (parameters) of various types.
    • Output: A function can return a value which is often stored in a variable. Some languages don't allow returning multiple values in a straightforward manner.
    • Side Effects: These are any changes a function makes to the program's environment, like printing to the screen (e.g., using printf), altering global variables, or modifying memory locations.

    Example

    • An example was provided of a function called power (likely a function calculating an integer exponent of another integer).

    Scope of Variables

    • Variables inside a function (local variables) are only accessible inside that function.
    • Arguments passed to functions are treated as temporary variables; changes inside the function don't affect the original variables outside.
    • Global variables can be accessed from within a function; but it's better to avoid them unless necessary for a specific case.
    • Example code was shown to demonstrate function scope in a program.

    About Functions

    • Functions can have different types of parameters, and possibly no parameters.
    • Functions can return void (meaning no explicit value returned). Functions don't need a return with an explicit keyword.
    • Functions need to be defined before being used.
    • Header files (such as stdio.h or math.h) are essential if you use pre-defined functions in a program.

    Side Effects

    • In an ideal case, a function is considered a black box; it accepts input and generates output without impacting other parts of the program.
    • External actions, such as printing to the console or writing to a file, are side effects.
    • Changes to global variables and modifications to memory are also side effects.

    Program Design

    • Functions are the building blocks of a program, and are used to solve different parts of a problem.
    • Program solutions can be generated by designing and implementing functions in a recursive way, i.e based on the functions used in the solution.

    Conclusion

    • Functions are crucial for writing organized, reusable, and understandable code.
    • Be mindful of side effects and variable scope.
    • Functions are a modular programming tool.

    Studying That Suits You

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

    Quiz Team

    Related Documents

    Lecture 6: Functions PDF

    Description

    This quiz focuses on the concept of functions in programming, particularly in the context of the course DT143G. It covers the importance of functions for code reusability, readability, and modularity. Additionally, it delves into parameters, arguments, and the role of functions in managing complex code.

    More Like This

    Use Quizgecko on...
    Browser
    Browser