Search code examples
csubroutine

C Hello World Program in a subroutine


#include <stdio.h>
#include <stdlib.h>

void message(char m)
{
print("Hello\n");
}

int main()
{
message(m);    
}

Error message when I try to compile

danielc@Ubuntu11:$ gcc Messagef.c -o Messagef
    Messagef.c: In function ‘main’:
    Messagef.c:11:9: error: ‘m’ undeclared (first use in this function)
    Messagef.c:11:9: note: each undeclared identifier is reported only once for each function it appears in

I know that am doing a 'silly' mistake but I just see where am going wrong


Solution

  • Your function takes a char parameter but never uses it. The simplest fix is to remove the unused parameter:

    #include <stdio.h>
    
    void message()
    {
        printf("Hello\n");
    }
    
    int main()
    {
        message();    
        return 0;
    }
    

    Alternatively, change your method to use the parameter, and pass in a character as an argument:

    #include <stdio.h>
    
    void message(char m)
    {
        printf("Hello%c\n", m);
    }
    
    int main()
    {
        message('!');    
        return 0;
    }
    

    See it working online: ideone