using a Void Function?

This is a discussion on using a Void Function? within the C Programming forums, part of the General Programming Boards category; Im new to C programming, so bear with me. I've created a couple of functions, but have never used one ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    7

    using a Void Function?

    Im new to C programming, so bear with me. I've created a couple of functions, but have never used one of the void type.
    Eventually I will have it draw a rectangle that displays 1 character across for every unit of width, and the same for the height, but first I just want to get the syntax correct just to make it print one character. Basically I just want to know how the void function works and understand how to use it.

    Code:
    void drawRect (int width, char character);    /*prototype*/
    
    
    printf ("%c\n", drawRect (width, character) );   /*function call*/
    
    
    
    void drawRect (int width, char character)       /*function*/
    {
    	if (width > 0)
    	{
    		printf ("%c\n",character);
    		
    	};
    	
    }
    These are all in the correct places in the code, because my other functions are working properly. When I compile, I keep getting this error 'printf' : actual parameter has type 'void' : parameter 2.

    Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    a function of type void (this is true in java, i'm assuming it's the same in C), it doesn't return anything.
    Brett Kelly

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by inkedmn
    a function of type void (this is true in java, i'm assuming it's the same in C), it doesn't return anything.
    You are correct.

    As such, this is the reason the original poster is getting an error.

    The way printf works is that for every format specifier you use, you have to be putting some value in it.

    When you use a function call as one of the parameters of printf, it expects it to be returning a value to put in the specifier you've matched it up with.

    Since your function is void, it doesn't return anything, and so, the format specifier doesn't have anything going into it.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    This is for a class, so our handout specifically says to use void....

    void drawRect (int width, int height, char character)

    If this isnt going to work, what are some examples of why you would use void?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Smoot
    This is for a class, so our handout specifically says to use void....

    void drawRect (int width, int height, char character)

    If this isnt going to work, what are some examples of why you would use void?
    Your function will work on its own, just don't put it as a parameter to a printf statement. That's your only problem, and I've explained why.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    12
    something like this:

    Code:
    void showMe(int i) {
        printf("my number: %d", i);
    }
    basic example (and i don't know if it works)
    Brett Kelly

  7. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    and to get you off on the right foot...never type

    Code:
    void main();
    Away.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    A guy at work explained it to me. All I had to do was use this: drawRect (width, character); as the function call, and eliminate the whole printf line.

    Thanks, quzah, now that I know how to do it your post makes sense. The problem is this is my first programming class and when I read your post I didn't really understand what you were telling me.

    Anyway it works now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  4. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21