Programming Techniques DT143G: Functions Lecture 6

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

Flashcards

Functions in Programming

Reusable blocks of code that perform specific tasks. Functions organize code, make it readable and allow for code reuse.

Function Parameters

Values passed into a function to be used in its calculations; similar to variables; used to customize the function's behaviour.

Function Arguments

The actual values provided to the function's parameters when the function is called.

Function Return Type

The data type of the value a function returns; tells you what kind of result the function computes.

Signup and view all the flashcards

Function Definition

A template that specifies the function's name, parameters, instructions and data type to return.

Signup and view all the flashcards

Function Inputs

The values passed into the functions for internal operation. Usually called parameters.

Signup and view all the flashcards

Function Output

The value a function returns to the calling part of the code.

Signup and view all the flashcards

Function Side Effects

Changes to the program's state caused by the function, other than the return value.

Signup and view all the flashcards

Local variables in functions

Variables declared inside a function. They exist only within that function.

Signup and view all the flashcards

Global variables

Variables declared outside of any function (within the file). Accessible from any function.

Signup and view all the flashcards

Function return type 'void'

Indicates that a function does not return any value.

Signup and view all the flashcards

Function prototype

A declaration of a function that specifies the function's name, return type, and parameters.

Signup and view all the flashcards

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

More Like This

Use Quizgecko on...
Browser
Browser