Podcast
Questions and Answers
What happens to modifications made to arguments passed into a function?
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?
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?
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?
Which of the following statements is true regarding the function power in the provided example?
What is a correct characteristic of a function that returns void?
What is a correct characteristic of a function that returns void?
What is one of the primary reasons for using functions in programming?
What is one of the primary reasons for using functions in programming?
Which statement best describes the idea of a 'black box' in relation to functions?
Which statement best describes the idea of a 'black box' in relation to functions?
What does the function in the provided example return?
What does the function in the provided example return?
What type of values can a function in C return?
What type of values can a function in C return?
In the example provided, what does the 'pow' function do?
In the example provided, what does the 'pow' function do?
Which of the following is true about side effects in functions?
Which of the following is true about side effects in functions?
What does the 'parameter_list' in a function definition signify?
What does the 'parameter_list' in a function definition signify?
What could happen if a function causes implicit modifications?
What could happen if a function causes implicit modifications?
Flashcards
Functions in Programming
Functions in Programming
Reusable blocks of code that perform specific tasks. Functions organize code, make it readable and allow for code reuse.
Function Parameters
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
Function Arguments
The actual values provided to the function's parameters when the function is called.
Function Return Type
Function Return Type
Signup and view all the flashcards
Function Definition
Function Definition
Signup and view all the flashcards
Function Inputs
Function Inputs
Signup and view all the flashcards
Function Output
Function Output
Signup and view all the flashcards
Function Side Effects
Function Side Effects
Signup and view all the flashcards
Local variables in functions
Local variables in functions
Signup and view all the flashcards
Global variables
Global variables
Signup and view all the flashcards
Function return type 'void'
Function return type 'void'
Signup and view all the flashcards
Function prototype
Function prototype
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
ormath.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.