MESSAGE
DATE | 2010-03-21 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C++ Workshop - Syntax Basics -- functions
|
A function is a variable, like any other variable, that contains code data instead of static numbers, binary data or characters. It represents a body of code that forms a process. All processes have input, output and side affects. The input of a function is its argument list. In C and C++, under most conditions values are passed by value. That means that the value of an argument of a function is copied into a function. There are important exceptions to this rule. It's output, like the output of any variable, is the type that is defined in the function declaration. Finally, functions have side affects.
Their is an operator affiliated with function, called the call operator, represented by the syntax "()", Functions are declared, defined and then instantiated. We will look at this in a moment.
Declaring Functions:
Functions are declared like any other variable with a couple of caveats. They are declared as a type and attached to a symbol, and like pointers or arrays, needs an operator, the call operator which also contains variable types that define the arguments:
int factorials(int, int); char * match_text(const * char, const *char); bool sort(double[], int);
These are all expectable function declarations.
It is usually important for functions to have broad scope so that it can be called in multiple parts of of your program. As such, they are best declared in large programs in your header files, which are then declared at the top of you code files. When declaring functions within your header file, unlike other variables, they do not need the keyword "extern" to be made global. In small programs they are often declared and defined simultaneously above main().
In most cases, functions are run time entities, that don't exist in your program until called. Variables and such that are declared within your function are not only local to the function, but but don't exist when the function ends. In fact, when a function process runs, the rest of your program most normally stops and the function exists as in its own space, inheriting certain environmental properties from the parent calling process, and then ends. This is called the runtime stack.
In traditional C programming, functions can only have one name, definition and declaration in your program. In C++, functions can be "overloaded", which means that they can have different parameter lists, each one being a attached to a different definition. This is very important for object oriented programming, as we will see.
A function definition includes a block of code that performs the tasks that your function will be responsible for. In procedural programming it is a good practice to define all the tasks of your program as appropriate function declarations, and then write your functions as needed.
Defining a function involves declaring the argument variables within the call operator and creating a statement block in curly braces which ends with the appropriate return type in a return statement. It's syntax looks like this:
func.h - Defining a struct, a struct type variables, a global int, and two functions.
#ifndef FUNC_H #define FUNC_H #endif /* FUNC_H */
struct flashcard { char * question; char * answer; };
extern flashcard * stack[]; extern const int size_of_stack;
flashcard ** quiz_setup(flashcard * [], const int); flashcard ** quiz_dump(flashcard * [], const int);
------------------------------------------------------ func.cpp - main() and the functions that define our problem
#include #include "func.h"
using namespace std;
int main(int argc, char * argv[]){ flashcard * stack[25]; flashcard ** deck; deck = quiz_setup(stack, size_of_stack); quiz_dump(deck, size_of_stack);
return 0;
} ~
func_lib.cpp - The definition of our functions ________________________________________________________________________
include "func.h" #include #include using namespace std;
const int size_of_stack=25;
flashcard ** quiz_setup(flashcard* stack[], const int size){ for(flashcard ** i = stack; i < stack + size; i++){ *i = new flashcard; }
for(int i = 0; i < size; i++){ stack[i]->question = new char [1000]; stack[i]->answer = new char [1000]; cout << "Enter a Question: " << endl; cin.getline(stack[i]->question, 1000,'\n'); cout << "Enter an Answer " << endl; cin.getline(stack[i]->answer, 1000,'\n'); } return stack; }
flashcard ** quiz_dump(flashcard* fulldeck[], const int size){ char pause; for(flashcard ** i = fulldeck; i < fulldeck + size; i++){ cout << "Question: " << (*i)->question << endl; cin >> pause; cout << "Answer: " << (*i)->answer << endl; cin >> pause; } return fulldeck; }
___________________________________________________________________
What is interesting about this program is it is the first time we are beginning to put together a variety of the programming tools that we have been studying. Look at the use of the global structure, and the use of the variable stack which is an array of pointers to struct. See if you can understand why we need to initialize the structures using the keyword "new" that we haven't covered yet, or how we utilize the variable "i" within the function flashcard ** quiz_dump(flashcard * [], const int) .
|
|