Thread: Need Help :programming with function

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Need Help :programming with function

    I tried to use switch statement with function into it.Cant make out where i made error .


    Program :
    Code:
    #include<stdio.h>
    void main()
    {
    int a,b,sum,mul;
    int i;
    printf("enter the value of a,b ");
    scanf("%d%d",&a,&b);
    printf("1.ADD 2.MULTIPLY");
    scanf("%d",&i);
    sum=add(a,b);/*function calling*/
    printf("the value is %d",sum);
    mul=pli(a,b);/*function calling*/
    printf("the value is %d",mul);
    }
    switch(i)/*quite confused with this part of code*/
    {
    case 1:
    add(int x, int y)/*fuction called*/
    {
    	int z;
    	z=x+y;
    	return(z);
    	break;
    }
    case 2:
    	pli(int k,int l)/*function called*/
    	{
    		int m;
    		m=k*l;
    		return(m);
    		break;
    
    }
    	}
    Error:
    Syntax error switch:
    Last edited by Techtree; 03-09-2010 at 06:25 AM.

  2. #2
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64

    Wink

    What is this ? Are trying to do some magic with switch .You can not use switch outside of the main or other functions . But in your code you used switch out of the block.
    Last edited by karthigayan; 03-09-2010 at 06:28 AM.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    66

    Question Why ?

    why switch case outside main function !!!

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    why not you prefer to call the function inside switch case instead of defining inside it.
    Have any specific reason !!!!

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    why not you prefer to call the function inside switch case instead of defining inside it.
    Got it .Thanks
    Last edited by Techtree; 03-09-2010 at 06:33 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you need to indent your code properly, e.g.,
    Code:
    #include<stdio.h>
    
    void main()
    {
        int a, b, sum, mul;
        int i;
    
        printf("enter the value of a,b ");
        scanf("%d%d", &a, &b);
    
        printf("1.ADD 2.MULTIPLY");
        scanf("%d", &i);
    
        sum = add(a, b); /*function calling*/
        printf("the value is %d", sum);
    
        mul = pli(a, b); /*function calling*/
        printf("the value is %d", mul);
    }
    
    switch(i)/*quite confused with this part of code*/
    {
    case 1:
        add(int x, int y)/*fuction called*/
        {
            int z;
            z = x + y;
            return(z);
            break;
        }
    case 2:
        pli(int k,int l)/*function called*/
        {
            int m;
            m = k * l;
            return(m);
            break;
        }
    }
    So, as has been pointed out, one problem is that you have a switch statement outside of function scope. It looks like you really want to define two functions named add and pli instead of having that switch statement. If so, you should declare those functions before the main function (which should have a return type of int). After that, you can re-introduce the switch into the main function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Yep. Noted Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM