Predefined Functions Revisited: Time Rand Srand Rand - Max
Predefined Functions Revisited: Time Rand Srand Rand - Max
what is?
function
function name
argument(s)
return value
function call
function invocation
nested function call
what are predefined functions?
what is the type of arguments and the type of return value and why are they important?
what is include directive and how is it related to predefined functions? linker?
what are type changing functions and why are they needed?
what is type casting? coersion?
what does function time() do?
what do functions rand() and srand() do? what is a seed? why is seed important for
random number generator?
what is RAND_MAX? how does one get a random number in a desired range?
1
Programmer-Defined Functions
3
Function Invocation Semantics
argument
invocation
caller – function that invokes another
callee – function that is being invoked
processing a function invocation: caller is suspended, callee is
executed, callee computes the return value which substitutes invocation
in the caller
if invocation is inside an expression, the return value is used to
evaluate the expression
4
Function Definition
function definition – specifies instructions that the function executes
consists of head, body
function body
return statement
5
Parameters and return-statement
programmer defined function cannot know what arguments will be passed to it;
it uses (formal) parameters
parameters - local variables of the callee that are initialized to the value of
arguments at invocation
6
Function Prototype
function prototype – declares (quickly introduces) the function
returnValue functionName(type parameterName,…,);
expanded form – mentions parameter types and names: names are optional
but sometimes desirable for clarity
int add1(int i);
abbreviated form – mentions only parameter types
int add1(int);
before function invocation, the compiler needs to see either its definition or
prototype
unlike variables, the function definitions and function prototypes should be put
outside any other function: no nested functions in C++ standard
it is (almost) possible to write a program without prototypes: put function
definitions before invocations
such program is hard to understand: more detailed functions would be ahead
in file
better program style - put function prototypes first, then main(), then other
functions with increasing level of detail
7
Commenting Functions
treat a function prototype as a variable definition:
append a short inline description of the function
precede a function definition with at least one line of
comments explaining the purpose of the function,
possibly comment on parameters
8
Function Terms Quick Review
what are the terms for the constructs in gray boxes?
10
Local Variables
variable that is declared inside a function is local (to that function): it cannot
be used outside of the function
the scope of such variable is from declaration till end of function
parameters are also local variables
local variables of two different functions are different even if they have the
same name
variables declared in main() are local to main()
example
// computes sum of integers in a..b
int sum(int a, int b) { // a and b are local
int total = 0; // this is a local variable
for (int i = a; i <= b; ++i) { // i is local to for
total += i;
}
return total;
}
11
Global Constants and Variables
constant or variable declared inside a function is local to this function
global constant – a constant declared outside any function definition
it can be used (its scope is) anywhere in the program from the place
it is declared
12
Call-by-Value
parameters are local variables of the function
when the function is called, the values of the arguments are evaluated and assigned to respective parameters
as any local variable, the value of a parameter can be changed in a function
this change does not affect the values of the original arguments
such discipline of parameter passing is call-by-value
int add1(int i) {
++i;
return i;
}
...
int n=5;
int newn = add1(n); // value of n is unchanged
13