PHYS 305: Computational Physics II

Some Simple Examples of Functions


  1. Function taking no arguments

    	int func()
    	{
    		int value;
    
    		...
    		...
    		...
    		value = ...;
    
    		return value;
    	}
    

  2. Function taking 1 or more arguments

    	float func(int a, float b, double c)
    	{
    		float value;
    		float x;
    
    		...
    		...
    		x = a + b - c;
    		...
    		...
    		value = x;
    
    		return value;
    	}
    

  3. Function returning no value

            void func(int a, float b, double c)
            {
                    ...
                    ...
                    ...
            }		/* (no return statement) */
    

  4. Function returning values through its arguments

            void func(int *a, float *b)
            {
                    ...
                    ...
                    *a = 5;      /* assign a value to scalar int a */
                    ...
                    b[3] = 7.5;  /* assign value to float array b  */
    
            }	             /* (no return statement) */
    
    
            int main()
            {
               int a, float b[10];
    
               func( &a, b );   /* specify variable addresses  */
                                /* name of an array IS the address of the array */
            }