C++/Function/Function Variables — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
(нет различий)
|
Текущая версия на 13:27, 25 мая 2010
Function local variable
<source lang="cpp">
- include <iostream>
using namespace std; void printMessage(void); int main () {
char choice; do { cout << "Enter Q to quit, any other character to continue: "; cin >> choice; if (choice == "Q") cout << "Input stopped"; else printMessage(); } while (choice != "Q"); return 0;
} void printMessage (void) {
int times = 0; times++; cout << "This function called " << times << " times\n";
}
</source>
Returning a Pointer to a Static Local Variable
<source lang="cpp">
- include <iostream>
using namespace std; char * setName(); int main (void) {
char* str = setName(); cout << str; return 0;
} char* setName (void) {
static char name[80]; cout << "Enter your name: "; cin.getline (name, 80); return name;
}
</source>
Static function variable
<source lang="cpp">
- include <iostream>
using namespace std; void printMessage(void); int main () {
char choice; do { cout << "Enter Q to quit, any other character to continue: "; cin >> choice; if (choice == "Q") cout << "Input stopped"; else printMessage(); } while (choice != "Q"); return 0;
} void printMessage (void) {
static int times = 0; times++; cout << "This function called " << times << " times\n";
}
</source>