Thread: Functions and Procedures

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    104

    Functions and Procedures

    Hi Everyone,
    I'm sure you saw other people ask this , but I need some help for my assignement .
    What it basically needs to do is this :

    1) Prompt for and then accept 10 floating point values from the
    user.
    2) Store these values in an array.
    3) Pass the array to a procedure which prints out the values to
    4 decimal places in a numbered fashion (i.e like line numbers).
    4) Pass the array to a function which returns the maximum value.
    5) The returned maximum value is printed out in scientific or
    exponential format along with a suitable heading.

    1 and 2 is no problem to do , but maybe someone can help point me in the right direction on passing the array to a procedure ( I'm a little more comfortable with the functions ) .

    If someone can explain thiscode for me please :
    Code:
    void blah(type *arr);
    ... 
    int main()
    {
        type array[10];
    ....
        blah(array);
    ...
    }
    
    void blah(type *arr) 
    {
       ...  ... arr[x] ... ... 
    }
    Aything to get me started . Thanks in advance

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by ICool View Post
    Aything to get me started . Thanks in advance
    Code:
    void blah(type *arr); /* this is the prototype of the function you are going to write */
    ... 
    int main()
    {
        type array[10]; /* this is an array of something */
    ....
        blah(array); /* here you pass the array to your function to do something with it */
    ...
    }
    
    void blah(type *arr) /* this is the function called in main in which you do something with the array you pass */
    {
       ...  ... arr[x] ... ... 
    }
    And the ... is where you write code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Part of me is concerned that the thread from which you found that code, you didn't bother to read, and yet a bigger part thinks that thread wasn't very helpful. So I'm going to try and help.

    To pass a flat array to a function all you really need to understand is that a function call such as

    min( a, size );

    will receive the array as a pointer to its first element. So if you wanted to write a prototype for min/max, you could write something to the effect of

    float min( float *a, unsigned int size );

    The second parameter I would think has an obvious purpose. You cannot find out the length of an array based on a passed pointer alone. Look at how strlen() handles it's parameter. It has to count a character array's elements until it finds a zero to return the correct length. It is of course, perfectly valid to have your algorithms work similarly to or depend on something like strlen() (loop until you know the size of the array because you found a distinct value). It is up to you to decide if that is efficient though.


    I'd just like to point out that most of your other tasks involve "formatted output with printf". If you google that, or look on cprogramming.com, you will find what you need.
    Last edited by whiteflags; 10-05-2007 at 11:49 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The term procedure is from Pascal (another programming language) and similar programming languages. It describes a subprogram that returns no value.

    The closest equivalent in C of a procedure is a function that returns void (i.e. returns nothing). So, in your sample code, the function blah() is effectively a procedure.

    Quote Originally Posted by ICool View Post
    Hi Everyone,
    If someone can explain thiscode for me please :
    Code:
    void blah(type *arr);
    ... 
    int main()
    {
        type array[10];
    ....
        blah(array);
    ...
    }
    
    void blah(type *arr) 
    {
       ...  ... arr[x] ... ... 
    }
    The dots in the code are garbage; I ignore their presence in the discussion below. In future, rather than using dots to indicate "more stuff here", and asking for people to describe what it does, it is better to provide working code samples.

    The first line is formally described as a "function declaration", although some people describe it as a "function prototype": it declares (i.e. tells the compiler) that there is a function (or procedure) named blah() that accepts a single argument that is a pointer to "type" and returns void. This allows the compiler to make sense of the situation (eg checking the correct number and type of arguments are passed) when subsequent code attempts to call blah(). For your code to work, your code will have to do something to ensure "type" describes something relevant.

    int main() defines the main entry point of your program. When you successfully create a program, main() is the first function called. The C compiler, linker, and runtime library (i.e. your development tools) take care of ensuring that main() is called. The body of main() is the stuff between the curly braces.

    The line "type array[10];" declares array to be a (well) array with ten elements of "type".

    The line "blah(array);" passes array as an argument to blah(). blah() will receive a pointer to the first element of array (i.e. a pointer that contains the address of array[0]). Formally, this is describes as "the name array is converted into a pointer to array[0]", and that pointer is passed to blah().

    The void blah() and the subsequent material between the curly braces at the bottom of your code is formally described as a definition of the function blah(). This is the implementation of the function blah(). Since there is a declaration of blah() above, it is formally an error if the definition does not match the previous declaration (eg if the definition works with arguments of different type, of a different number of arguments, or a different return type).

    The line arr[x] assumes that the pointer arr is an array, and access arr[x]. (I am presuming here that x is an integral value). For the call blah(array) above, the access arr[x] will access array[x] if x is in the range 0 to 9. If x is outside the range 0 to 9, the result will be undefined behaviour (trying to access the 20th element of an array of 10 elements a problem with your code). "Undefined behaviour" means anything is allowed to happen (eg a program crash, nothing, some unrelated variable in your program changing randomly and unexpectedly, etc etc).

    I'll leave other problems you have (eg printing out values, finding the maximum element in an array, etc) as something for you to investigate. Providing answers to those concerns would strike me as doing your homework.

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Grumpy, Citizen and Dave thank you for your help and information about the topic. I went away and came up with some code which worked fine until recently , but now all of a sudden stopped spitting out the 10 numbers I have inputted. I have no idea why it stopped working . The function correctly identifies the largest number , but the procedure ill not spit out the 10 values. I would appreciate if someone can take a look :
    Code:
    #include <stdio.h>
    FILE * output =NULL;
    FILE * input =NULL;
    
    
    float function_maximum(float array[10])			//Function that works out the largest number 
    
    {
    
    	int i;
    	float maximum_number=0;
    	for(i=0; i<10; i++)
    	
    	{
    
    	if(array[i]>maximum_number)
    		{
    		maximum_number=array[i];
    		}
    	}
    	
    	fprintf(output,"The largest number entered is %E",maximum_number);
    	
    
    }
    
    
    void print_array(float array[10])			// Procedure to print out the array contents
    
    {							// Initiate a variable j to go through the array contents
    	int i;
    	for(i=0; i<10; i++)
    
    	{
    		fprintf(output,"%.4f\n ",array[i]);	// Prints out the array values, numbers with 4 decimal places	
    	}
    	
    }
    
    int main(void)
    
    {
    
    
    	int i;
    	int where_up_to=1;
    	float array[10];
    
    	output = stdout; 
       	input = stdin;
    
    	for(i=0; i<10; i++) // For loop will fetch 10 values from the user
    
    	{
    
    		if(where_up_to==1)	// This will add a correct suffix  based on where the loop is up to.
    	
    			{
        				fprintf(output,"Enter %dst value\n" ,where_up_to); 
        				fscanf(input,"%d",&array[i]);
    				where_up_to=where_up_to+1;
    			}
    	
    			else if((where_up_to==2)||(where_up_to==3))
    
    			{
    			    	fprintf(output,"Enter %dnd value\n" ,where_up_to); 
        				fscanf(input,"%d",&array[i]);
    				where_up_to=where_up_to+1;
    			}
    
    			else
    	
    			{
    			
        				fprintf(output,"Enter %dth value\n" ,where_up_to); 
        				fscanf(input,"%d",&array[i]);
    				where_up_to=where_up_to+1;
    			}
    
    	}
    
    	
    	print_array(array);
    	function_maximum(array);
    	
    
    
    }
    Once again , thank you for all your help .

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Use "%f" to fscanf a float, not "%d".
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. send() and recv() functions
    By kris.c in forum Networking/Device Communication
    Replies: 9
    Last Post: 06-24-2006, 09:41 PM
  2. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  3. Member functions as child window procedures
    By filler_bunny in forum Windows Programming
    Replies: 28
    Last Post: 03-15-2004, 09:45 AM
  4. Class Procedures
    By Marky_Mark in forum C++ Programming
    Replies: 4
    Last Post: 10-27-2001, 05:03 AM
  5. functions ??? help
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2001, 02:33 AM