Passing functions call by value?

This is a discussion on Passing functions call by value? within the C Programming forums, part of the General Programming Boards category; I was doing a book example showing passing function pointers. Out of curiosity I took out the pointer syntax and ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Passing functions call by value?

    I was doing a book example showing passing function pointers. Out of curiosity I took out the pointer syntax and it still works. Does that mean that the function is being passed call by value?


    Code:
    #include <stdio.h>
    
    #define SIZE 10
    
    void bubble( int [], const int, int (*)( int, int ) );
    int ascending( int, int );
    int descending( int, int );
    
    int main()
    {
    
    	int order,
    	    counter,
    	    a[ SIZE ] = { 2, 6, 4, 8, 10, 12 , 89, 68, 45, 37 };
    
    	printf( "Enter 1 to sort in ascending order,\n"
    		   "Enter 2 to sort ind escending order: " );
    	scanf( "%d", &order );
    	printf( "\nData items in original order\n" );
    
    	for ( counter = 0; counter < SIZE; counter++ )
    		printf( "%5d", a[ counter ] );
    
    	if ( order == 1 ) {
    		bubble( a, SIZE, ascending );
    		printf( "\nData items in ascending order\n" );
    	}
    	else {
    		bubble( a, SIZE, descending );
    		printf( "\nData items in descending order\n" );
    	}
    
    	for ( counter = 0; counter < SIZE; counter++ )
    		printf( "%5d", a [counter ] );
    
    	printf( "\n" );
    
    	return 0;
    }
    
    
    //-----------------------------------------------It was ( *compare )( int, int )------------------------------
    void bubble( int work[], const int size, int (compare)( int, int ) )
    {
    	int pass, count;
    
    	void swap( int *, int * );
    
    	for ( pass = 1; pass < size; pass++ )
    
    		for ( count = 0; count < size - 1; count++ )
    
    			if ( (compare)( work[ count ], work[ count + 1 ] ) )
    				swap( &work[ count ], &work[ count + 1 ] );
    }
    
    
    
    void swap( int *element1Ptr, int *element2Ptr )
    {
    	int temp;
    
    	temp = *element1Ptr;
    	*element1Ptr = *element2Ptr;
    	*element2Ptr = temp;
    }
    
    
    
    int ascending( int a, int b )
    {
    	return b < a;
    
    }
    
    
    int descending( int a, int b )
    {
    	return b > a;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's a piece of syntactic sugar -- any function parameter that is itself a function is actually interpreted as a pointer to function (much like your int work[] is actually converted to int *work in the same prototype there). And there's a corresponding piece of syntactic sugar on the other side -- even though compare is a function pointer, you don't need to dereference it to call the function to which it points; using it as a function call is enough.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,596
    As as for tips...
    I suggest not putting prototypes inside functions. They go to the top of the file or inside headers; nowhere else.
    Then, do not strip the parameter names from the prototypes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 09:13 AM
  2. Replies: 2
    Last Post: 07-03-2008, 11:31 AM
  3. Multiple types in lists, vectors or arrays.
    By megatron09 in forum C++ Programming
    Replies: 20
    Last Post: 08-31-2006, 01:54 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. Replies: 4
    Last Post: 03-31-2003, 11:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21