Originally Posted by
blueeyes
I there anyway of preserving that value instead of adding any new variable just keeping the int main() same.
i have changed pointer to val in return and understood what your point here however if is there any other way without changing int main()
Yes, of course!
Call doubleValue(), and incValue() by value, not by pointer, and return the calculated value. Then call the functions as arguments to the printf() statements.
You still need to add error checking code for the scanf() to insure that scanf() work correctly. What if I typed in "abc" as input to the program? ;^)
Code:
#include <stdio.h>
void getNumber(int *input)
{
printf("Enter an integer number:");
scanf("%d",input); // Error checking needed!!!
}
int incValue(int val)
{
if( val>1 && val<100 )
{
val+=10;
}
else if (val>101 && val<1000){ // && Not &
val+=20;
}
else{
val+=25;
}
return val; // Return the value
}
int doubleValue(int val)
{
return val *= 2;
}
// void main(void)
int main(void) //main() ALWAYS returns an int!!!
{
int number = 0; // Always initialize all local variables
getNumber(&number);
printf("That value doubled is %d \n", doubleValue(number)); // Added newline
printf("That value incremented is %d \n", incValue(number)); // Added newline
return 0; // Added
}