Argumente

Ein Funktionsaufruf kann gar kein Argument oder eines oder mehrere aufweisen. Beispiel: Das hier funct(zahl); ist ein Funktionsaufruf mit einem Argument.

Ich nenne die Reihe von Argumenten die "Eingänge" in die Funktion, oder den "Eingang". Denn die Argumente oder das Argument, falls es nur eines gibt, dient ja nur dem Zweck, der Funktion etwas zu übergeben.

Das folgende kleine Programm weist zwei Parameter auf. Der erste Parameter ist dies: "char druckbaresZeichen"; der zweite dies: "int anzahl".

//reihe2.cpp
#include <iostream>//cout

using namespace std;

void zeichenreihe(char, int);//Funktionsdeklaration

int main()
{
    //Erster Funktionsaufruf:
    zeichenreihe('-', 32);
    cout << endl << "C++ :                  seit 1979";
    cout << endl << "               Bjarne Stroustrup";
    //Zweiter Funktionsaufruf:
    zeichenreihe('+', 32);
    //Dritter Funktionsaufruf:
    zeichenreihe('*', 38);
    cout << endl;
    system("pause");
    return 0;
}//Ende von main()

//Funktionsdefinition:
void zeichenreihe(char druckbaresZeichen, int anzahl)
{
    cout << endl;
    for (int i=0; i<anzahl; i++)
      cout << druckbaresZeichen;
}//Ende von zeichenreihe()

Eine Funktion mit zwei Argumenten kann auch so aussehen.

Drei Argumente: Hanoi

//hanoi1.cpp
#include <iostream>//cout
using namespace std;

void hanoi(int, int, int);
int anzahl;

int main()
{
    cout << "Turm von Hanoi .......";
    cout << endl << "Bitte fertigen Sie 3 bis 5 verschieden grosse Papierscheiben.";
    cout << endl << "(Das Spiel mit 3 Scheiben ist leicht und schnell; das Spiel";
    cout << endl << "mit 6 Scheiben dauert laenger und ist schon schwieriger.) Auf die";
    cout << endl << "Scheiben schreiben Sie 1 bis 3 (oder 1 bis 4, oder 1 bis 5, oder 1 bis 6).";
    cout << endl << "Die kleinste Scheibe bekommt die 1, die groesste die 6.";
    cout << endl << "Dann brauchen Sie einen DIN-A4-Bogen Papier.";
    cout << endl << "Schreiben Sie genau die drei Zahlen 1, 2 und 3 auf den Bogen,";
    cout << endl << "mit etwas Abstand. Nun duerfen Sie alle Scheiben auf Platz 1 legen.";
    cout << endl;
    system("pause");
    cout << endl << "Die groesste Scheibe kommt nach unten und so weiter! Im Spiel:";
    cout << endl << "Die kleinste Scheibe mit der 1 liegt immer oben.";
    cout << endl << "Niemals darf eine kleinere von einer groesseren verdeckt werden.";
    cout << endl << "Verboten: Alle oder mehrere Scheiben auf einmal verschieben;";
    cout << endl << "Auch verboten: Eine oder mehrere Scheiben unter Scheiben wegziehen.";
    cout << endl << "Alle Scheiben sind zu Beginn auf Platz 1.";
    cout << endl << "Scheibe 1 kommt dann von Platz 1 auf Platz 2 oder 3.";
    cout << endl << "Ziel: Alle Scheiben kommen am Ende auf Platz 3 zu liegen.";

    cout << endl << endl;
    system("pause");
    do
    {
        cout << endl << "Wieviele Scheiben wollen Sie (3 bis 6)? ";
        cin >> anzahl;
    } while (anzahl<3 || anzahl>6);
    hanoi(anzahl, 1, 3);//Ziel: Platz 3.
    cout << endl;
    system("pause");
    return 0;
}//main

void hanoi(int n, int ap, int zp)
{
    if (n>1)
     hanoi(n-1, ap, 6-ap-zp);
    cout << endl << "Scheibe " << n << " von " << ap << " nach " << zp;
    if (n>1)
      hanoi(n-1, 6-ap-zp, zp);           
}//hanoi-Funktion


Argument oder Parameter

Argumente oder Parameter? Man sagt "Argument" beim Funktionsaufruf, während "Parameter" in der Funktionsdefinition stehen. Siehe auch:

I like this quote from MSDN: "...the procedure defines a parameter, and the calling code passes an argument to that parameter. You can think of the parameter as a parking space and the argument as an automobile." – Michiel van Oosterhout Dec 16 '14 at 21:24 (bei stackoverflow gefunden)


Snake Game

//snake.cpp im Internet gefunden
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <ctime>
using namespace std;

void run();
void printMap();
void initMap();
void move(int dx, int dy);
void update();
void changeDirection(char key);
void clearScreen();
void generateFood();

char getMapValue(int value);

// Map dimensions
const int mapwidth = 20;
const int mapheight = 20;

const int size = mapwidth * mapheight;

// The tile values for the map
int map[size];

// Snake head details
int headxpos;
int headypos;
int direction;

// Amount of food the snake has (How long the body is)
int food = 3;

// Determine if game is running
bool running;

int main()
{
    srand(time(NULL));
    cout << "Taste druecken, bitte; es geht dann sofort los." << endl;
    getch();
    run();
    return 0;
}

// Main game function
void run()
{
    // Initialize the map
    initMap();
    running = true;
    while (running) {
        // If a key is pressed
        if (kbhit()) {
            // Change to direction determined by key pressed
            changeDirection(getch());
        }
        // Upate the map
        update();

        // Clear the screen
        clearScreen();

        // Print the map
        printMap();

        // wait 0.5 seconds
        _sleep(500);
    }

    // Print out game over text
    cout << "\t\t!!!Game over!" << endl << "\t\tYour score is: " << food;

    // Stop console from closing instantly
    cin.ignore();
}

// Changes snake direction from input
void changeDirection(char key) {
    /*
      W
    A + D
      S

      1
    4 + 2
      3
    */
    switch (key) {
    case '8':
        if (direction != 2) direction = 0;
        break;
    case '6':
        if (direction != 3) direction = 1;
        break;
    case '2':
        if (direction != 4) direction = 2;
        break;
    case '4':
        if (direction != 5) direction = 3;
        break;
    }
}

// Moves snake head to new location
void move(int dx, int dy) {
    // determine new head position
    int newx = headxpos + dx;
    int newy = headypos + dy;

    // Check if there is food at location
    if (map[newx + newy * mapwidth] == -2) {
        // Increase food value (body length)
        food++;

        // Generate new food on map
        generateFood();
    }

    // Check location is free
    else if (map[newx + newy * mapwidth] != 0) {
        running = false;
    }

    // Move head to new location
    headxpos = newx;
    headypos = newy;
    map[headxpos + headypos * mapwidth] = food + 1;

}

// Clears screen
void clearScreen() {
    // Clear the screen
    system("cls");
}

// Generates new food on map
void generateFood() {
    int x = 0;
    int y = 0;
    do {
        // Generate random x and y values within the map
        x = rand() % (mapwidth - 2) + 1;
        y = rand() % (mapheight - 2) + 1;

        // If location is not free try again
    } while (map[x + y * mapwidth] != 0);

    // Place new food
    map[x + y * mapwidth] = -2;
}

// Updates the map
void update() {
    // Move in direction indicated
    switch (direction) {
    case 0: move(-1, 0);
        break;
    case 1: move(0, 1);
        break;
    case 2: move(1, 0);
        break;
    case 3: move(0, -1);
        break;
    }

    // Reduce snake values on map by 1
    for (int i = 0; i < size; i++) {
        if (map[i] > 0) map[i]--;
    }
}

// Initializes map
void initMap()
{
    // Places the initual head location in middle of map
    headxpos = mapwidth / 2;
    headypos = mapheight / 2;
    map[headxpos + headypos * mapwidth] = 1;

    // Places top and bottom walls
    for (int x = 0; x < mapwidth; ++x) {
        map[x] = -1;
        map[x + (mapheight - 1) * mapwidth] = -1;
    }

    // Places left and right walls
    for (int y = 0; y < mapheight; y++) {
        map[0 + y * mapwidth] = -1;
        map[(mapwidth - 1) + y * mapwidth] = -1;
    }

    // Generates first food
    generateFood();
}

// Prints the map to console
void printMap()
{
    for (int x = 0; x < mapwidth; ++x) {
        for (int y = 0; y < mapheight; ++y) {
            // Prints the value at current x,y location
            cout << getMapValue(map[x + y * mapwidth]);
        }
        // Ends the line for next x value
        cout << endl;
    }
}

// Returns graphical character for display from map value
char getMapValue(int value)
{
    // Returns a part of snake body
    if (value > 0) return 'o';

    switch (value) {
        // Return wall
    case -1: return 'X';
        // Return food
    case -2: return 'O';
    }
}