Calling to function

This is a discussion on Calling to function within the C Programming forums, part of the General Programming Boards category; Example: Code: 1 printf ("Enter name: \n"); 2 scanf ("%s", &name); 3 printf ("Enter phone: \n"); 4 scanf ("%d", &phone); ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    11

    Calling to function

    Example:

    Code:
    1   printf ("Enter name: \n"); 
    2   scanf ("%s", &name); 
    3   printf ("Enter phone: \n"); 
    4   scanf ("%d", &phone); 
    5   add_field(name,phone); 
    6   .... 
    7   add_field(a,b) 
    8 
    9   char a; 
    10   char b; 
    11 
    12   { 
    13      printf ("%s - name\n", name); 
    14      printf ("%d - phone\n", phone); 
    15   }
    When I try to compile this...
    Error 5: Extra parameter in call to add_field()

    How can I correct it?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Without seeing all of the code I would have to say that add_field doesn't take 2 parameters as you have passed to it. Look at the decleration of add_field to determine the parameters.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    full code

    Code:
    #include "stdio.h"
    
    char name;
    char phone;
    
    int main()
    {
    
    	int choice; /* promenliva za izbor */
    	int clear; /* izchistva <CR> */
    	float add_field();
    	printf ("Menu: \n");
    	printf ("Add field (1)\n");
    	printf ("Del field (2)\n");
    	printf ("Update field (3)\n");
    	printf ("View all fields (4)\n");
    	printf ("Exit (5)\n");
    	scanf ("%d", &choice);
    	switch(choice)
    	
    	{
    	
    		case 1 :
    		
    			printf ("Enter name: \n");
    			scanf ("%s", &name);
    			printf ("Enter phone: \n");
    			scanf ("%d", &phone);
    			add_field(name,phone);
    			break;
    
    		case 2 :
    
    			printf ("asd");
    			break;
    
    		case 3 :
    
    			printf ("asd");
    			break;
    
    		case 4 :
    
    			printf ("asd");
    			break;
    
    		default :
    
    			printf("Ok. Thanks for using me. Goodbye");
    			return 0;
    
           }
    }
    
    add_field(char *a, char *b)
    {
    	printf ("%s - name\n", name);
    	printf ("%d - phone\n", phone);
    }

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Your defining add_field as taking no parameters and then redefining it later to take two parameters, so not suprisingly the compiler is complaining.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    so will u tell me how to correct this? what I must do?

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    There are many other things you would need to do besides correcting the definition of the "add_field" function.

    1.) Allocate the memory for the variable "name" and declare it appropriately.

    2.) phone is of type "int" rather than "character"

    3.) function declaration should be changed to
    float add_field(char *, int);


    Dunno why do you want to call it add_field and why is your return value of the type float.
    I would rather declare it as

    void display_fields (char *, int);

    Anoop.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    I'be correct the source code...

    Code:
    #include "stdio.h" 
    
    void add_field();
    char name[20];
    int phone;
                    
    int main() 
    { 
    
    	int choice; /* promenliva za izbor */ 
    	int clear; /* izchistva <CR> */ 
    	printf ("Menu: \n"); 
    	printf ("Add field (1)\n"); 
    	printf ("Del field (2)\n"); 
    	printf ("Update field (3)\n"); 
    	printf ("View all fields (4)\n"); 
    	printf ("Exit (5)\n"); 
    	scanf ("%d", &choice); 
    	switch(choice) 
        
    	{ 
        
    		case 1 : 
            
    			printf ("Enter name: \n"); 
    			scanf ("%s", &name); 
    			printf ("Enter phone: \n"); 
    			scanf ("%d", &phone); 
    			add_field(name,phone);
    			break; 
    
    		case 2 :
    
    			printf ("asd"); 
    			break; 
    
    		case 3 : 
    
    			printf ("asd"); 
    			break; 
    
    		case 4 : 
    
    			printf ("asd"); 
    			break; 
    
    		default : 
    
    			printf("Ok. Thanks for using me. Goodbye"); 
    
    	}
    	return 0;
    }
    
    void add_field(char *n, int phone)
    {
    	printf ("%s - name\n", n);
    	printf ("%d - phone\n", phone);
    }
    ...but the same error occur...

  8. #8
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    Code:
    #include "stdio.h" 
    
    void add_field(); //change to: void add_field(char *, int);
    char name[20];
    int phone;
    But anoopks already wrote that.

    And change

    scanf ("%s", &name);

    to

    scanf ("%s", name);

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    I've alredy correct my code. Now all is OK. Thanks for your help.

  10. #10
    Registered User terryrmcgowan's Avatar
    Join Date
    Jun 2003
    Posts
    8

    hope this helps

    Spedge,

    I've had a little play with your code and made some changes that make it work. I'm too new at this to tell you weather what I've done is good coding practice but it should give you an idea of where your heading.

    PS: If there are any glaring errors or just bad practices in my code, I be grateful if someone would let me know.

    Code:
    #include "stdio.h"
    
    void add_field(void);//must include function prototype
    
    
    char name[20];  //made these arrays of char rather than single chars
    char phone[20];
    
    int main()
    {
    
    	int choice,a; /* promenliva za izbor (int a added to keep window open)*/
    	int clear; /* izchistva <CR> */
    	/*float add_field();*///dont know what this is for?
    	printf ("Menu: \n");
    	printf ("Add field (1)\n");
    	printf ("Del field (2)\n");
    	printf ("Update field (3)\n");
    	printf ("View all fields (4)\n");
    	printf ("Exit (5)\n");
    	scanf ("%d", &choice);
    	switch(choice)
    	
    	{
    	
    		case 1 :
    		
    			printf ("Enter name: \n");
    			scanf ("%s", &name);
    			printf ("Enter phone: \n");
    			scanf ("%s", &phone);    //changed %d integer to %s string
    			add_field();//arrays defined globally so no arguments needed
    			break;
    
    		case 2 :
    
    			printf ("asd");
    			break;
    
    		case 3 :
    
    			printf ("asd");
    			break;
    
    		case 4 :
    
    			printf ("asd");
    			break;
    
    		default :
    
    			printf("Ok. Thanks for using me. Goodbye");
    
    
           }
           scanf("%d",&a);/*used by me to keep window open*/
           return 0;/*return taken out of switch statement*/
    }
    
    void add_field(void)   //tell compiler no arguments taken or returned
    {
    	printf ("%s - name\n", name);
    	printf ("%s - phone\n", phone);//again %d made %s
    }

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    >>>PS: If there are any glaring errors or just bad practices in my code, I be grateful if someone would let me know.


    Just that it would be nice to do away with as many global variables as possible. And hence you could declare the function as

    void add_field( char *, char *);

    and declare variables "name" & "phone" inside main and pass them as arguments.


    Anoop

  12. #12
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    scanf ("%s", &name);
    scanf ("%s", &phone);

    name and phone are pointers. Thus, no address-of operator & is needed.

    scanf ("%s", name);
    scanf ("%s", phone);

    btw: good idea to change phone to char string. I forgot about phone numbers starting with 0.

  13. #13
    Registered User terryrmcgowan's Avatar
    Join Date
    Jun 2003
    Posts
    8

    Cheers Anoop

    thanks Anoop,

    Ordinarily I wouldn't use global variables, but I took Spedge's code and just tried to make it work, without really considering how I'd go about that problem,(it's the pressure of trying to read the board, figure out what the problem is and convince my boss that I really am working hard, given that my job has nothing to do with programming).
    Once again Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM

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