![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Feb 2005
Posts: 89
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() |
#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. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#5 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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("*");
} |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2005
Location: Bethany Beach, DE
Posts: 10
Rep Power: 0
![]() |
that was a nice explaination ooble. thanks a lot. works perfect
|
|
|
|
|
|
#7 | |
|
Programming Guru
![]() |
Quote:
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.
__________________
|
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|