Functions Flashcards
A function is
is a named list of statements.
A function definition consists of the new function’s name and a
block of statements. Ex: double CalcPizzaArea() { /* block of statements */ }
A function call is
an invocation of a function’s name, causing the function’s statements to execute.
The function’s name can be any valid identifier. A block
is a list of statements surrounded by braces.
A function may return one value using a
return statement. Below, the ComputeSquare() function is defined to have a return type of int; thus, the function’s return statement must have an expression that evaluates to an int.
return numToSquare * numToSquare;
A parameter is a
function input specified in a function definition. Ex: A pizza area function might have diameter as an input.
An argument is a
value provided to a function’s parameter during a function call. Ex: A pizza area function might be called as CalcPizzaArea(12.0) or as CalcPizzaArea(16.0).
Write a statement that calls a function named CalcCalories, passing the value 3 as an argument.
CalcCalories(3);
A parameter, like a variable declaration,
cannot be an expression.
A function definition may have multiple parameters, separated by commas. Parameters are assigned with argument values by position: First parameter with first argument, second with second, etc.
A function definition with no parameters must still have
the parentheses, as in: int DoSomething() { … }. The call must include parentheses, with no argument, as in: DoSomething().
Which correctly passes two integer arguments for the function call:
CalcVal(…)?
(99, 45 + 5)
Each argument evaluates to an integer.
The number of parameters and arguments must
match
A function declaration specifies the function’s
return type, name, and parameters, ending with a semicolon where the opening brace would have gone. A function declaration is also known as a function prototype.
Sometimes a function’s last parameter (or last few) should be optional. A function call could then omit the last argument, and instead the program would use a default value for that parameter. A function can have a
default parameter value for the last parameter(s), meaning a call can optionally omit a corresponding argument.
For global scope should only ( ) should be declared
constants
It is not considered good programming to declare global variables.
True