Allgemeine Unterfunktionen



  • Hallo zusammen ich habe folgendes Programm geschrieben. Nun möchte ich nicht alles in die main Funktion klatschen. wie deklariere und rufe eine Unterfunktion auf ? Das int Funktion(); war mein Versuch

    oh hier mein Code. Mit int funktion2() wollte ich die unterfunktion dann aufrufen, die das selbe beinhaltet nur andere Sätze
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int funktion2();
    int main(int argc, const char * argv[]) {

    const char *satz1 = "Hallo wie geht es dir";
    char satz1neu[30];
    int repeat,k,a,b,abfrage;

    printf("This is a programm to learn the some German sentences \n\n");
    printf("Please pay attention to small and capital letters \n\n");

    printf("The first sentence is\n\n");

    printf("Hello how are you doing ? \n\n");
    printf("On German: ");
    puts(satz1);
    printf("\n");

    for(a=0;a<b;a++)
    {
    b=1;

    printf("Could you repeat the sentence? \nPress 1 for Yes and 2 for No: \n ");
    scanf("%d",&repeat);
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

    if(repeat<2)
    {
    printf("Okay please enter the complete sentence: \n");

    scanf(" %29[^\n]", satz1neu);

    if((strcmp(satz1,satz1neu))==0)
    {

    printf("Perfect!!!\n");
    }

    else
    {
    while(k!=1)
    {
    printf("That is wrong.\n");
    printf("Try it again!\n\n");

    scanf(" %29[^\n]", satz1neu);

    if((strcmp(satz1,satz1neu))==0)
    {
    printf("Perfect!!!\n");

    k=1;
    }
    }
    }

    }

    else

    {
    printf("The sentence is: ");
    puts(satz1);

    }

    printf("Press 1 to repeat the sentence and 2 to try the next sentence: \n");
    scanf("%d",&abfrage);

    if(abfrage==2)
    {
    a++;
    }

    b++;

    }

    int funktion2();

    return 0;
    }



  • Beispiel:

    #include <stdio.h>
    
    void funktion1()
    {
       puts("Funktion1: Ich bekomme keine Parameter und liefere kein Ergebnis\n\n");
    }
    
    void funktion2(int in)
    {
       printf("Funktion2: Ich bekomme Parameter %d\n\n", in);
    }
    
    int funktion3(int a, int b)
    {
       printf("Funktion3: Ich bekomme Parameter %d und %d und liefere die Summe zurueck\n\n", a, b);
       return a + b;
    }
    
    int main()
    {
       funktion1();
       funktion2(2);
       funktion2(5);
    
       printf("%d\n", funktion3(2, 4));
       printf("%d\n", funktion3(5, 8));
    }
    


  • Richtig wäre dies:

    void funktion1()
    {
       puts("Funktion1: Ich bekomme eine unspezifizierte Anzahl Parameter und liefere kein Ergebnis\n\n");
    }
    

    Oder eher wahrscheinlich:

    void funktion1(void)
    {
       puts("Funktion1: Ich bekomme keine Parameter und liefere kein Ergebnis\n\n");
    }
    

Anmelden zum Antworten