Functions
What type of data can a function return
A function can return any type of data except an array. If a non-void function ends because it reaches the last curly brace, it can cause an error. IE if you have defined a function as returning an interger value (e.g. int calcmyage(1965)) then you should ensure that it returns an integer value by explicitly including the return statement.
What is a function
A function is a subroutine that contins 1 or more C++ statements and performs a specific task. Every program has at least 1 function: main(). Functions are referred to as the building blocks of C++ because a program is a collection of functions.
What is a prototype for a function
A function is declared using a 'prototype', prior to it's definition (i.e. prior to the function itself) or use. The prototype must be before main() which is predefined by C++ and does not need its own prototype. Eg void myfunc(); //or int mycalcfunc(int a, int b,int c); //etc.
Explain Local Scope
A local scope is created by a block (ie between curly braces). A variable that is declared inside a block is called a local variable and can only be used by statements located within the block in which it is declared. The variable is created when it is declared within the block and destroyed upon exit from the block. Thus it cannot be used by other statements outside the block.
Using return to end a function
A simple function can be ended with just the closing curly brace. You can also end a function with the statement return; This causes execution to return immediately to the caller. Any code remaining in the function is ignored. Typically, this would be used if there is an error, eg if the parameter yearborn is negative. You can also return a value, a useful way to get information from a function: return(value); This can only be used with functions that do not return void, and must be of the type already declared for the function. [Remember a function cannot return an array]. e.g. answer = calcmyage(1965); // the return value from the function will be assigned in the main program to answer cout << "Your age is " << answer; int calcmyage( int yearborn) //start of function { return 2014 - yearborn }
No really, what is an Argument
A value passed to a function is called an argument. Its a way to get information into a function, so that the function can use it. When you create a function that takes 1 or more arguments, variables that will receive these arguments must also be declared. These variables are called the parameters of the function.
Whats the difference between an argument and a variable
A value passed to a function is called an argument. Usually, the value that is passed will be a variable in the program calling the function (but it could be a constant e.g. a number) For the function, the value is copied into a variable which is termed a parameter. E.g. if the function calcmyage() calculates my age and needs to be passed the year I was born in, it could be declared as void calcmyage(int yearborn); In the program you would call the function like this: calcmyage(1965); 1965 is the argument and yearborn is the parameter (accessible within the function only). The value is the same for each i.e. 1965.
Explain Local Scope and a function
A variable that is declared inside a block is called a local variable and can only be used by statements located within the block in which it is declared. A function defines a block of code that begins with the function's opening { and ends with its final }.A function's code and data are private to that function and cannot be accessed by any statement in any other function except through a call to that function. The contents of one function are completely separate from the contents of another. The code and data that are defined within one function cannot interact with the code or data defined in another function, because the 2 functions have a different scope. Because each function defines its own scope, the variables declared within one function have no effect on those declared in another in another - even if those variables share the same name. Thus you can use i as a counter in 2 separate functions and they are 2 completely separate 'i's. Because a local variable is created and destroyed with each entry and exit from the block in which it is declared, a local variable will not hold its value between activations of its block. When you call a function a second time, all of the variables that had been used within it the first time are created anew.
How do you call a function
In main(), specify the function name followed by parentheses. When a function is called, program execution jumps to the function. Execution continues inside the function untilits closing curly brace is encountered. Ehen the function ends, program execution returns to the caller at the statement immediately following the function call.
What must be included in a recursive function
It must have some form of conditional statement, such as an if, somewhere to force the return without execution of the recursive call. If you don't provide the conditional statement, then once you call the function, it will never return (like a never-ending echo).
What is an Argument
Its when 2 people shout at each other. Oh no it isn't. Oh yes it is. It's not. Is.
Where can local variables be declared
Local variables can be declared anywhere, in any block of code. A variable declared within a block is local to that block. No code outside that block - including other code within the same function - can access that variable. E.g if(x == 19) { int y = 20; cout << x + y; } cout y; // ERROR - y does not exist outside the block in which it was declared. It is common to declare all of the variables needed in a function at the beginning of that function's code block. This helps in reading the code and understanding the variables, but it is not required that be declared at the start. However, as per above, the start of the function is within the function's code block and therefore the scope of the variables is limited to the function.
Why use recursion
Most recursive routines don't significantly reduce code size, and can even be a little bit slower than their non-recursive (i.e. iterative) equivalents. However, they can be clearer and simpler than iterative equivalents in some cases.
What is recursion
Sometimes called circular definition, recursion is the process of defining something in terms of itself. As it relates to programming, recursion is the process of a function calling itself.
Why should you not declare all variables with global scope
Storage for global variables is in a fixed region of computer memory set aside for this purpose by the program. Therefore you should only use global variables where you need them to be accessible throughout the program (e.g. to 2 different functions) because 1) They take up memory the entire time the program is executing, not just when needed 2) Using a global variable when a local one would do makes a function less general, more specific. It relies on variables outside of itself and so has a more complex relationship with the program. It would for example, be more difficult for a programmer to copy the function and use it in a different program.
What is Global Scope
The Global Scope is the declarative region outside of all functions (including outside of main()). Variables declared here can be used by all functions. Usually variables with global scope are declared at the top of the program, before main(), but they can be declared anywhere provided that it is outside of all functions. Global variables can be used by any piece of code and they maintain their values throughout the entire execution of the program. A global variable does not need to be (should not be) passed as an argument to functions in order for them to use it - they can just go ahead because it has global scope throughout the program.
Give an example of recursion
The function factr(), which computers the factorial of an integer, is recursive. The factorial of N is the product of all the whole number between 1 and N. When using the factr() function, say to get the factorial of 4, it calls itself to get the factorial of 3, which calls itself to get the factorial of 2, which calls itself to get the factorial of 1.
The main () function
The main() function is special - it is the first function called when your program executes. It does not have to be at the top of the program, but usually is to make it easier to understand. There is only 1 main() in a program. As it is a special, pre-defined one, it does not need a function prototype (i.e. to be declared in advance).
What is the scope of Function Parameters
The parameters to a function are within the scope of the function. thus, they are local to the function. Except for receiving the values of the arguments, parameters behave like any other local variables.
What happens in a recursive function
When a function calls itself, new local variables and parameters are allocated storage, and the function is executed with these new variables from the start. As each recursive call returns, the old local variables are removed, and execution resumes at the point just after the recursive call inside the function. Recursive functions could be said to 'telescope' out and back. [Or be thought of like Russian dolls]
Can you use a function in an expression
Yes. You don't need to store a function's result in a variable before using it, you can directly include a function (as long as its return type is not void) in an expression e.g. answer = howoldami(1965); cout << answer; is ok, but the following is slightly more efficient cout << howoldami(1965);
What is the general form of a function
return-type name(parameter-list) { // body of function } return-type specifies the type of data returned by the function. This can be any valid type (e.g. int or char), except an array. If the function doesn't return a value, its return-type must be void. The name can be any legal identifier not already in use. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that receive the value of the arguments passed to the function when it is called. It is ok for a function not to have any parameters ie. for the parameter list to be empty.