C function return

Example - Not return anything

If the function does not return any value Set the return data type as void.


#include <stdio.h>
/*from   w  w  w  .ja v  a  2s  .  c om*/
void add() {
   printf("asdfasdf");
}
main ()
{
    add();       
    add();       
    add();       
}

The code above generates the following result.

Example - return int from function


#include <stdio.h>
#include <stdlib.h>
// www. ja va  2  s  .c om
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);
}

The code above generates the following result.

Example - return pointer from function


#include <stdio.h>
//from w  w w. j  av a 2s.co  m
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;                        
}

The code above generates the following result.





















Home »
  C Language »
    Language Advanced »




File
Function definition
String
Pointer
Structure
Preprocessing