C Tutorial/Function/Function Return
Functions That Return value
#include <stdio.h>
#include <stdlib.h>
int getval(void);
int main()
{
int weight;
weight=getval();
return(0);
}
int getval(void)
{
char input[20];
int x;
printf("some integer:");
gets(input);
x=atoi(input);
return(x);
}
some integer:123
If the function does not return any value
Set the return data type as void.
#include <stdio.h>
void add() {
printf("asdfasdf");
}
main ()
{
add();
add();
add();
}
asdfasdfasdfasdfasdfasdf
Return value as pointer
#include <stdio.h>
long *myFunction(long* pPay);
int main(void)
{
long your_pay = 30000L;
long *pold_pay = &your_pay;
long *pnew_pay = NULL;
pnew_pay = myFunction( pold_pay );
printf("\nOld pay = $%ld", *pold_pay);
printf(" New pay = $%ld\n", *pnew_pay);
return 0;
}
long *myFunction(long *pPay)
{
*pPay += 10000L;
return pPay;
}
Old pay = $40000 New pay = $40000