Click to See Complete Forum and Search --> : Procedure in C
wmHardRock
10-14-2000, 10:28 PM
scanfHow can I have procedure in C? Here's a simple example of what I'd like to do:
/* Program */
procedure Ask_name
{
scanf("%s",x);
}
main()
{
ask_name;
}
:strain:
10-14-2000, 10:32 PM
you mean like
int narf();
int narf()
{
//do some stuff
printf("hello. my name is bob\n");
}
int main()
{
narf;
}
?
dunno
10-14-2000, 10:54 PM
You mean a function?
The syntax is:
return_value function_name( arg1, arg2, etc) {
... /* stuff */
}
for example:
/* Program */
void ask_name() {
scanf("%s", x);
}
int main() {
ask_name();
}
TheLinuxDuck
10-14-2000, 11:14 PM
Originally posted by wmHardRock:
scanfHow can I have procedure in C? Here's a simple example of what I'd like to do:
So, you want to call a function that asks for some data? You'd be better of using something like fgets. scanf is bad news. http://www.linuxnewbie.org/ubb/smile.gif
#include <stdio.h>
// Pass in the pointer to the string to fill,
// the max amount of chars allowed, and a
// message to display. Return a pointer to
// the string, or a NULL, if they hit
// enter on a blank line
char *getinput(char *text,short int max,const char *message);
int main(void)
{
char fullname[50];
// Display the text if entered
if(getinput(fullname,50,"What is your full name?")!=NULL)
printf("you put: %s\n",fullname);
return 0;
}
char *getinput(char *text,short int max,const char *message)
{
printf("%s: ",message);
fgets(text,max,stdin);
// Fgets always appends a newline char to the
// string, whether they entered anything or
// not.
if(text[0]=='\n') return(NULL);
text[strlen(text)-1]='\0';
return(text);
}
Is that what you are looking for?
------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq
wmHardRock
10-15-2000, 05:45 PM
OK, now that it works, what do void and int before the functions mean? How do you know what to put?
wmHardRock
------------------
"Hi could you print my report please? My printer is broken here."
"What was your username again?"
# cd ~user
# grep 'gay' mbox > /etc/motd
# cat /dev/null > phd_final_report
# userdel user
"It's not there; probably due to electro-magnetic rains provoked by solar instability"
Mikenell
10-15-2000, 06:20 PM
They're the return values, void means it has nothing returned to it, and int means it has an integer returned.
Mikenell
yeah, check out the snippets LD gave you... when you type return VARIABLE in a function, it's going to return the value stored in VARIABLE. The return type that is given right before the function name (like the void and int you mentioned) must be the same as the type of VARIABLE. If you don't use the return statement in your function (because in some case you may not wish to return anything), then the return type is void.