CS1111 Functions Ch.5

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

Function scope

Function scope indicates that a variable is active and visible from the beginning to the end of a function. In C, only the goto label has function scope. For example, the goto label, start, shown in the following code portion has function scope: int main() { int i; /* block scope */ . . . start: /* A goto label has function scope */ . . . goto start; /* the goto statement */ . . . return 0; }

File scope

In C, a global variable declared with the static specifier is said to have file scope. A variable with file scope is visible from its declaration point to the end of the file. Here the file refers to the program file that contains the source code. Most large programs consist of several program files.R

Auto storage class specifier

The auto specifier indicates that the memory location of a variable is temporary. In other words, a variable's reserved space in the memory can be erased or relocated when the variable is out of its scope. Only variables with block scope can be declared with the auto specifier. The auto keyword is rarely used, however, because the duration of a variable with block scope is temporary by default.

Static storage class specifier

The static specifier, on the other hand, can be applied to variables with either block scope or program scope. When a variable within a function is declared with the static specifier, the variable has a permanent duration. In other words, the memory storage allocated for the variable is not destroyed when the scope of the variable is exited, the value of the variable is maintained outside the scope, and if execution ever returns to the scope of the variable, the last value stored in the variable is still there. int main() { int i; /* block scope and temporary duration */ static int j; /* block scope and permanent duration */ . . return 0; }

Register Specifier

The word register is borrowed from the world of computer hardware. Each computer has a certain number of registers to hold data and perform arithmetic or logical calculations. Because registers are located within the CPU (central processing unit) chip, it's much quicker to access a register than a memory location. Therefore, storing variables in registers may help to speed up your program.

Extern Specifier

Use the extern specifier provided by the C language to allude to a global variable defined elsewhere. In this case, we declare a global variable in file A, and then declare the variable again using the extern specifier in file B.

Block scope

a block refers to any sets of statements enclosed in braces ({ and }). Thus, the variable is active and accessible from its declaration point to the end of the block. Sometimes, block scope is also called local scope. int main() { int i; /* block scope */ . . . return 0; }

Variables declared in a block or in the parameter list of a function are assumed to be of storage class _________ unless specified otherwise.

auto

The storage-class specifiers are ________ , _________, _________ and _________.

auto, register, extern, and static

A recursive function typically has two components: one that provides a means for the recursion to terminate by testing for a(n) __________ case, and one that expresses the problem as a recursive call for a slightly simpler problem than the original call.

base

A non-static variable defined outside any block or function is a(n) ________ variable.

external or global

A program module in C is called a(n)

function

A function is invoked with a(n)

function call

A(n) _________ allows the compiler to check the number, types, and order of the arguments passed to a function.

function prototype

The four possible scopes of an identifier are ________,________ , _________ and _______.

function scope, file scope, block scope, and function-prototype scope

A variable that's known only within the function in which it's defined is called a(n)

local variable

The ________ function is used to produce random numbers.

rand

A function that calls itself either directly or indirectly is a(n) _________ function.

recursive

The __________ statement in a called function is used to pass the value of an expression back to the calling function

return

The three ways to return control from a called function to a caller are ________ , ________ and __________.

return; or return expression; or encountering the closing right brace of a function

The __________ of an identifier is the portion of the program in which the identifier can be used.

scope

The ________ function is used to set the random number seed to randomize a program.

srand

For a local variable in a function to retain its value between calls to the function, it must be declared with the ___________ storage-class specifier.

static

Keyword __________ is used in a function header to indicate that a function does not return a value or to indicate that a function contains no parameters.

void


Kaugnay na mga set ng pag-aaral

Chapter 4: Documentation and Interprofessional Communication

View Set

Honan, Chapter 15: Nursing Management: Patients With Complications From Heart Disease

View Set

Review Questions - Network Defense and Countermeasures - SEC 210 - Intrusion Detection

View Set

Chapter 2: The Data of Macroeconomics

View Set

The Glorious Revolution, England's Bill of Rights, and European Art Culture: AP Euro, pp. 474-485

View Set

Insurance Exam: Chapter 3, Life Insurance Test, LIFE INSURANCE POLICIES

View Set

Chapter 16 - Strategic Elements of Competitive Advantage

View Set

Educational Psychology and Development of Children and Adolescents - D094 --- Module 9 Test

View Set