SDS322 Functions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What all does a function consist of?

1) Function result type 2) Function name 3) Zero or more function parameters -these consist of a type and name as well -separated by commas 4) Function body that consists of statements 5) Return statement

What can the scope be limited by?

An occurrence of a variable with the same name

Where do default arguments go when writing a function code?

At the very end of the parameter list

(T/F) A function can have only one return result

False

(T/F) A function can only have one input

False

(T/F) A void function can not have a return statement

False. Because it wont have any output/result

What is the scope?

How long the variable lasts. Can be local or global

In general, the use of the function name has to be in the ___ ___ of a variable and after its ___?

Lexical scope Definition

Output of this code? bool something{true}; int i = 3; if ( something ) { int i = 5; cout << "Local: " << i << endl; } cout << "Global: " << i << endl; if ( something ) { float i = 1.2; cout << "Local again: " << i << endl; } cout << "Global again: " << i << endl;

Local: 5 Global: 3 Local again: 1.2 Global again: 3

Polymorphic function?

Multiple functions with the same name

Is this a valid program? void f() { i = 1; } int main() { int i=2; f(); return 0; }

No, because i is undefined

Global variable: Variable declared where? The scope of the variable...?

Outside the function Extends to the end of the program

A function body defines a ___?

Scope

Output of this code? bool something{false}; int i = 3; if ( something ) { int i = 5; cout << "Local: " << i << endl; } cout << "Global: " << i << endl; if ( something ) { float i = 1.2; cout << i << endl; cout << "Local again: " << i << endl; } cout << "Global again: " << i << endl;

Since bool something{false}, everything in the conditionals with "something" is not true so it wont be output. Global: 3 Global again: 3

What is a reference?

Something with an ampersand in its definition and it acts as an alias of the thing it references

What does void mean?

That the function will not return any results back to the main program

What does this code represent? void f() { ... } int main() { int i; f(); cout << i;

That the variable cannot be reached even though its present in memory.

void f(int &n) { n=... } int main () { int i; f(i); } What is the value of i?

The value of i is what n equals since it is a reference to the variable i

A function call copies....?

The value of the function argument into the function parameter

(T/F) Functions have to be defined before you can use them

True

(T/F) The local variables of the function calculations are invisible to the calling program?

True

(T/F) The purpose of functions is to make your code shorter

True

(T/F) Using functions makes your code easier to read.

True

Recursion?

When a function calls itself it becomes a recursive function

Shadowing?

When the variable is invisible for awhile bc its shadowed. After the lifetime of the shadowing variable, its value is unchanged

Local variable: Variable declared where? Evaluated where? The scope of the variable is...?

Within the body of the function. Only within the function A function where its defined

Output? #include <iostream> using std::cout; using std::endl; int main() { int i=5; if (true) { i = 6; } cout << i << endl; return 0; }

i = 5 Because inside the curly bracket, i=6 is not defined

What is the output of this program? #include <iostream> using std::cout; using std::endl; int main() { int i=2; i += /* 5; i += */ 6; cout << i << endl; return 0; }

i = 8 because everything from /* 5; i += */ is commented out so what is left is int i=2; i+= 6;

Output? int add1(int i) { return i+1; } int main () { int i =5; i = add1(i); cout << i << endl; }

i =6

Output? void add1(int i) { i = i+1; } int main () { int i=5; add1(i); cout << i << endl; }

i=5

Output? int add1(int &i) { return i+1; } int main () { int i =5; i = add1(i); cout << i << endl; }

i=6

Output? void add1(int &i) { i = i+1; } int main () { int i=5; add1(i); cout << i << endl; }

i=6 since ampersand is in front of i

#include <iostream> using std::cout; using std::endl; int main() { int i=5; if (true) { int i = 6; } cout << i << endl; return 0; }

i=6 because it is now defined with int in front of it


Kaugnay na mga set ng pag-aaral

Southern Adventist University, Personal Finance, Quiz 5

View Set

Trigonometry: The Pythagorean Theorem 201

View Set

NMNC 4410 Cellular Regulation Quiz

View Set

02.08 Isolation Precaution Types (PPE)

View Set