Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 16th, 2005, 12:42 PM   #1
bliznags
Newbie
 
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0 bliznags is on a distinguished road
void functions in C

i am very new to programming and have been faced with a problem i can not figure out. i first wrote a program to print out:

****
****
****
****

and I was successful with it. the code is as follwed:

#include <stdio.h>
int main(){

int i;

for(i=0; i<16; i++)
{
if(i % 4 == 0)
printf("\n");
printf("*");
}
printf("\n");
}

but now i have to use this same program and write a void function that prints out a single asterik and use it inside the loop instead of printf.

any help will be appreciated.
bliznags is offline   Reply With Quote
Old Mar 16th, 2005, 12:49 PM   #2
brkstf
Programmer
 
brkstf's Avatar
 
Join Date: Feb 2005
Posts: 89
Rep Power: 4 brkstf is on a distinguished road
this is pretty simple

you just declare your prototype
void makeAsterisk();

then, you have to write the actual function
void makeAsterisk()
{
code goes here
}

and then everytime you want to print the asterisk, you call the function.

maybe you're overthinking it. it's more about learning the syntax than doing the work.
brkstf is offline   Reply With Quote
Old Mar 16th, 2005, 12:57 PM   #3
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
#include <stdio.h>

void printAst();
void printNL();
void printPattern(int count, int brk);

int main(int argc, char *argv[]) {
    if(argc != 3) {
        printf("Usage %s count break\n", argv[0]);
        exit(0);
    }

    printPattern(atoi(argv[1]), atoi(argv[2]));

    return 0;
}

void printAst() {
    printf("*");
}

void printNL() {
    printf("\n");
}

void printPattern(int count, int brk) {
    int i;

    for(i=1;i<=count;i++) {
        printAst();
        if((i%brk) == 0) printNL();
    }
}
__________________


Last edited by tempest; Mar 16th, 2005 at 1:02 PM.
tempest is offline   Reply With Quote
Old Mar 16th, 2005, 5:08 PM   #4
bliznags
Newbie
 
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0 bliznags is on a distinguished road
still confused

hey guys thanks for the help. Tempest, your code is way too advanced for me to follow, I have not learned most of the things included in it but thank you.

brkstf, i am somewhat following what youre saying, but i'm brand new to the world of coding. with what you said this is what i came up with (please dont laguh)

#include <stdio.h>
void printAst();

int main(){

int i;

for(i=0; i<16; i++)
{
if(i % 4 == 0)
printf("\n");
return 0;

void printAst(){
printf("*");
}
}
printf("\n");
}


this does not work. i get the following errors.
"lab04.6.c", line 14: syntax error before or at: void
"lab04.6.c", line 16: warning: end-of-loop code not reached
"lab04.6.c", line 18: syntax error before or at: "\n"
"lab04.6.c", line 18: warning: old-style declaration or incorrect type for: printf
"lab04.6.c", line 18: warning: identifier redeclared: printf
current : function() returning int
previous: function(pointer to const char, ...) returning int : "/usr/include/iso/stdio_iso.h", line 179


my textbook is crap and tells me nothing about void functions. furthermore, my professor is extremely vague when describing concepts.

Last edited by bliznags; Mar 16th, 2005 at 5:11 PM.
bliznags is offline   Reply With Quote
Old Mar 16th, 2005, 5:38 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
The function goes outside the main function:
#include <stdio.h>

void printAst();

int main() {
    int i;
    
    for(i = 0; i < 16; i++) {
        PrintAst();
        if (i % 4 == 0)
            printf("\n");
    }
    
    return 0;
}

void printAst() {
    printf("*");
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Mar 17th, 2005, 12:46 AM   #6
bliznags
Newbie
 
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0 bliznags is on a distinguished road
that was a nice explaination ooble. thanks a lot. works perfect
bliznags is offline   Reply With Quote
Old Mar 17th, 2005, 12:56 AM   #7
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Quote:
Originally Posted by bliznags
Tempest, your code is way too advanced for me to follow,

Sorry about that, it wasn't intentional. What happened was i created one that i was going to post, and decided to test it to make sure it worked. As i was doing that i added in command-line arguments in a different file (or so i thought) and overwrote the one i was going to cut and paste into the forum. Without looking i posted the wrong code, stupid of me.
__________________

tempest is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:46 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC