Thread: function syntax error

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    function syntax error

    this is a prog to search an array of random numbers. i'm trying to use recursion, but something is wrong with the function f_search. can anyone help? thanks again

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    //functions
    int f_search(int, int, int);
    
    main()
    {
    	int const array_size=400;
    	int array[array_size], holder, key;
    //get key
    	cout<<"enter an int from 1 to "<<array_size*2<<endl;
    	cin>>key;
    
    //seed
    	srand(time(0));
    
    //load aray
    	for (int i=0; i<array_size; i++)
    	{	array[i]=1+rand() %array_size*2;
    	}
    	
    //sort array
    	for (int i=0; i<array_size; i++)
    	{	for (int x=0; x<array_size; x++)
    		{	if (array[x]<array[x-1] && x!=0)
    			{	holder=array[x];
    				array[x]=array[x-1];
    				array[x-1]=holder;
    			}}}
    
    //display array
    	for (int i=0; i<array_size; i++)
    	{	cout<<array[i]<<" ";
    	}
    
    //search
    	f_search(int array[], int array_size, int key);//error: expressino syntax
    
    	return(0);
    }
    
    int f_search(int a, int size, int key)
    {
    	int location=4;//still working on, 4 is dummy value
    	if (size/2 == key)
    	{	cout<<"the number you picked ("<<key<<") was found in element "<<location;
    		return 1;
    	}
    	else
    		f_search(int a, int size/2, int key);//error: expressino syntax
    }

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    f_search(int a, int size/2, int key);//error: expressino syntax
    You shouldn't be using data types in the call to f_search, just use the arguments.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    :blush: i didn't put those types in there i swear it, it must have my evil computer. :abscond:

    thanks

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >f_search(int array[], int array_size, int key);
    This is not the syntax for a function call, function calls simply use the name of the variable, not the type. The problem here and inside the f_search function is that the compiler thinks you are trying to prototype a function without a return value. This should work better:
    f_search(array, array_size, key);

    The next problem is that you declare f_search to take an int as the first parameter yet you try to pass it an array. In the declaration you need to change
    int f_search(int , int, int);
    to
    int f_search(int *, int, int);

    And it will clear that error. Also, your function needs to return a value if the key is not found in the array.

    C_Coder: You beat me this time

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    ok, i made the noted changes (thanks guys). but now i get this error:

    c:\mprog\search.cpp:
    Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
    Error: Unresolved external 'f_search(int *, int, int)' referenced from C:\BORLAND\BCC55\BIN\SEARCH.OBJ

    it looks like it built, but couldn't compile? here's the code just incase

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    
    //functions
    int f_search(int *, int, int);
    
    main()
    {
    	int const array_size=400;
    	int array[array_size], holder, key;
    //get key
    	cout<<"enter an int from 1 to "<<array_size*2<<endl;
    	cin>>key;
    
    //seed
    	srand(time(0));
    
    //load aray
    	for (int i=0; i<array_size; i++)
    	{	array[i]=1+rand() %array_size*2;
    	}
    	
    //sort array
    	for (int i=0; i<array_size; i++)
    	{	for (int x=0; x<array_size; x++)
    		{	if (array[x]<array[x-1] && x!=0)
    			{	holder=array[x];
    				array[x]=array[x-1];
    				array[x-1]=holder;
    			}}}
    
    //display array
    	for (int i=0; i<array_size; i++)
    	{	cout<<array[i]<<" ";
    	}
    
    //search
    	f_search(array, array_size, key);//error: expressino syntax
    
    	return(0);
    }
    
    int f_search(int a, int size, int key)
    {
    	int location=4;//still working on, 4 is dummy value
    	if (size/2 == key)
    	{	cout<<"the number you picked ("<<key<<") was found in element "<<location;
    		return 1;
    	}
    	else
    		return f_search(a, size/2, key);//error: expressino syntax

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Make sure that your function definition is the same as your prototype.
    int f_search(int *, int, int);

    int f_search(int, int, int)

    With the names removed, here's the difference. The top is your prototype, the bottom is the definition.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    got it, thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM