thats what my first post did
theyre called functions and what they do is what you want.
Code
#include <stdio.h>
void function( int x)
int main()
{
//main code
function(x);
return 0;
}
void function( int x)
{
//do stuff with the x variable that was passed to the function
}
Code
#include <stdio.h>
return_type function_name(variable_type variable_name) <--function prototype (you need to declare this above your main entry point else you cant use the function and youll get errors)
int main()
{
//main code
function(x);
return 0;
}
return_type function_name(variable_type variable_name)
{
//do stuff with the x variable that was passed to the function
}
the functions return type is what type of data it returns. in this example the return type is void meaning nothing. if i changed it to INT it would return integer values. if i changed it to char it would return character values.
the variable type and name is the kind of variables you want to pass to the function for the function to use.
functions allow you to export code and to organize it more clearly.
you can also create your own libraries by exporting your functions to their own .c file and then create a .h file with the same name as the .c and include the function prototypes in the .h file then in your main() file you can do #include <xxxx.h> and use the functions.
Code
int check_number(int var)
{
char cstring[25];
sprintf(cstring, "%d", var)
for(int i = 0; i < 25; i++)
{
if(!isdigit(cstring[i])) return 1;
}
return 0;
}
this code works like this
1) put the integer into the cstring
2) loop through the string and see if the current character is a digit. if it is return with "yes"
3) if the entire string does not contain a alpha character return with "no"
the code you were testing on was flawed in the way of the test sample "1a" would return "no" if you used my break method.
edit:: and in any function when you issue a "return" the rest of the code after it will not run. example: if you tried to pass 11a to it it would only loop 3 times instead of 25 and will return "yes" or w/e
This post was edited by AbDuCt on Jul 7 2012 10:35pm