130 likes | 198 Views
Predefined Functions Revisited. what are predefined functions? what is: ? function name argument(s) return value function call function invocation what is the type of arguments and the type of return value and why are they important?
E N D
Predefined Functions Revisited • what are predefined functions? • what is: ? • function name • argument(s) • return value • function call • function invocation • what is the type of arguments and the type of return value and why are they important? • what is an include directive and how is it related to predefined functions? • what are type changing functions and why are they needed? • what is type casting? • what does the function time() do? • what do functions rand() and srand() do? What is a seed? Why is a seed important for random number generation?
Functions • Functions are named portions of code • Two types of functions: • predefined - provided in libraries for the benefit of all programmers • programmer-defined - written by a programmer • To carry out its task the function accepts arguments • To use a function the programmer writes the function name and a list of arguments for the function to use. This is called a function call (or function invocation) • every function returns a result in a return value • a function call can be used in any place an expression or statement is used • if a function is used as an expression - the function evaluates to its return value • if a function is used as a statement - the return value is ignored • arguments and return value of a function are of their specified types.
Function Invocation argument cout << add1(4+5)+6 << endl; invocation • caller – function that invokes another • callee – function that is being invoked • at a function invocation, the caller is suspended and the callee is executed, the callee computes the return value which is then substituted in place of the invocation. • if the invocation is inside an expression, the return value is used to evaluate the expression.
Programmer-Defined Functions • A programmer defined function cannot know what arguments will be passed to it; it uses (formal) parameters • The formal parameters are given the values of the arguments in the sequence in which they are listed • A programmer-defined function needs to be described by the programmer:this description consists of two parts • function prototype • function definition The function prototype – gives the type of the return value, the name of the function and the types of the parameters in that order: It may, or may not give names to the parameters 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); • if no parameters – use keyword void int exampleFunc(void);
Function Definition • function definition – specifies instructions, the function executes • consists of head, body function name parameter return type double circleArea (double r) { const double PI = 3.14159; return PI * r * r; } function head function body return statement
Return Statement • The return-statement specifies what value the function returns: returnexpression; • the expression is evaluated, converted to the type specified in function head (watch out!) and the function terminates • a return-statement is optional. If a function does not have a return-statement it terminates when the last statement is executed. The returned value is unspecified • technically, a function can have multiple return statements; but it is advisable to have just one at the end of the function - such a function is easier to read
What are the names of the elements in gray boxes? Ch 6/ Foil 8 #include <iostream> double CircleArea(double r); // computes circle area // manage circle computation int main() { cout << "Enter radius: "; double MyRadius; // circle radius cin >> MyRadius; double Area = CircleArea(MyRadius); cout << "Circle has area " << Area; } // computes area of radius r circle double CircleArea(double r) { const double PI = 3.1415; return PI * r * r; }
Declaring a Function, Style • before calling a function it needs to be declared: either a function prototype or a function definition declares the function • unlike variables, function declarations and function prototypes should be put outside other functions – The C++ standard does not allow nested functions. • technically you do not need function prototypes - just put all the function definitions first • this would result in a program structure where functions implementing details go first and more abstract functions follow - these programs are hard to read and understand • appropriate program style - put function prototypes first, thenmain() then the functions with an increasing level of detail • commenting functions • treat a function prototype as a variable definition - append a short description of the function • precede a function definition with at least one line of comments explaining what the function is doing and what the parameters are for.
Local Variables • a variable that is declared inside a function is local. It cannot be used outside the function • the scope of such variable is from its declaration untill the function ends • The parameters are also local variables • local variables of two different functions are different even if they have the same name note that variables declared in main() are local to main() as well (in fact, main() is just another function; the only thing special about it is the name.) // computes sum of integers in a..b range int sum(int a, int b) { // parameters are local int total = 0; // total is a local variable for (int i = a; i <= b; ++i) total += i; return total; }
Global Constants and Variables • the same constants may be used in multiple functions. • if a constant is declared outside any function it is called global and it can be used (its scope is) anywhere in the program from the place it is declared const double PI = 3.14159; const double TAX_RATE = 0.05; // 5% sales tax • similarly one can declare global variables. Global variables can be used and modified in any function of the program: int errorcode = 0; • using global variables makes your program hard to understand and debug and should be avoided • global constants and variable declarations should be grouped and placed at the beginning of your program
Simple Programs • single file structure • include statements • using statements • function prototypes • function definitions
Call-by-Value • formal 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 • this discipline of parameter passing is called call-by-value