Javada Fonksiyonlar ve Pointersal İşlemler

Pointer kullanmadan yazılan fonksiyon

#include <stdio.h>

int cubeByValue( int n );

int main()
{
   int number = 5;

   printf( "The original value of number is %d", number );
   
   /* pass number by value to cubeByValue */
   number = cubeByValue( number );

   printf( "nThe new value of number is %dn", number );

   return 0;
}

int cubeByValue( int n ) {
   return n * n * n;  
}
 

 

 Pointer kullanılarak yukardaki fonksiyonun tekrar yazılması

#include <stdio.h>
void cubeByReference( int *nPtr );
int main()
{
   int number = 5;
   printf( "The original value of number is %d", number );
   cubeByReference( &number );
   printf( "nThe new value of number is %dn", number );
   return 0;
}
void cubeByReference( int *nPtr )
{
   *nPtr = *nPtr * *nPtr * *nPtr;  /* cube *nPtr */
}
 
The original value of number is 5
The new value of number is 125

 

Fonksiyon İşağretcisi

#include <stdio.h>
int sum(int x, int y)
{
  return x + y;
}
int product(int x, int y)
{
  return x * y;
}
int difference(int x, int y)
{
  return x - y;
}
int main(void)
{
  int a = 10;
  int b = 5;
  int result = 0;
  int (*pfun)(int, int);              /* Function pointer declaration      */
  pfun = sum;            
  result = pfun(a, b);                /* Call sum() through pointer        */
  printf("npfun = sum             result = %d", result);
  pfun = product;        
  result = pfun(a, b);                /* Call product() through pointer    */
  printf("npfun = product         result = %d", result);
  pfun = difference;      
  result = pfun(a, b);                /* Call difference() through pointer */
  printf("npfun = difference      result = %dn", result);
  return 0;
}
 
pfun = sum             result = 15
     pfun = product         result = 50

     pfun =

Yorumunuzu Ekleyin

Yükleniyor...