3 Replies - 277 Views - Last Post: 09 October 2012 - 01:22 PM Rate Topic: -----

#1 koji52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 25-September 12

Error: Void Function

Posted 08 October 2012 - 05:55 PM

Hello guys...newbie wants to learn programming here. This time I am practicing how to use Void, i searched the web on how to use it. They say its a returning value? but there's no clear way to show how to return to something or its just me. so here's my code: :red_indian:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>

int main()
{
    int x;
    char y[]="",choice[2];
    int opt;
void menu_y(void);
{    
    system("cls");
    printf("1.add");printf("\n");
    printf("2.subtract");printf("\n");
    printf("3.Multiply");printf("\n");
    printf("4.Exit");printf("\n");
    
    
    
    printf("Enter: ");scanf("%d",&opt);
    system("cls");
}    
    switch(opt)
    {
               case 1:
                    
                    printf("Addition!!\n");
                    printf("\n\n\t\t\t\tReturn to Main [Y/N]" );scanf("%s",&choice);
                    system("cls");
                    if(strcmpi(choice,"y")==0)
                    {
                    menu_y();
                    }
                    else if (strcmpi(choice,"n")==0) 
                    {
                         printf("\n\n\n\n\t\t\t\tSAVING DATA.");
                         Sleep(300);
                         system("cls");
                         p("\n\n\n\n\t\t\t\tSAVING DATA..");
                         Sleep(300);
                         system("cls");
                         printf("\n\n\n\n\t\t\t\tSAVING DATA...");
                         Sleep(600);
                         system("cls");
                         
                         printf("\n\n\n\n\t\t\t\tEXITING WINDOWS...");
                         Sleep(600);
                         system("cls");
                                               
                         return 0;
                    }
                    else 
                    {
                         printf("invalid Choice!");
                         printf("\n\nEXITING WINDOWS");
                         Sleep(1500);
                         return 0;
                    }
                    break;
                    
               case 2:
                    
                    printf("Subtraction\n");
                    system ("pause");
                    menu_y();
                    break;
                    
               case 3:
                    
                    printf("Multiply\n");
                    //menu();
                    system ("pause");
                    menu_y();
                    break;
                    
               case 4:
                    
                    return 0;
                    break;     
               
    default: printf("Nothing!\n");
             system ("pause");
             menu_y();
    }
    system("pause");
    
}



basically i want to return to the void menu_y so i could choose another option.
this is the error that came up 'void value not ignored as it ought to be'
thanks in advance guys.

Is This A Good Question/Topic? 0
  • +

Replies To: Error: Void Function

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: Error: Void Function

Posted 08 October 2012 - 05:59 PM

You have tried to define a function inside another function, that is not allowed. Move your menu_y() function implementation to outside all other functions.


Jim
Was This Post Helpful? 0
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Error: Void Function

Posted 08 October 2012 - 11:13 PM

Basically, a function is written like this.

return_type function_name(parameters if any)
{
//some code
}



Now, a function with return type as void will not return anything at all. Eg.

void return_nothing()
{
printf("Hello, I don't return anything, hence the name ");
}



When a function has a return type, say int, then it will definitely return an integer value.Eg.

int return_int()
{
//sum of 2 numbers
int num1=1, num2=2,sum;

sum=num1+num2;

return sum;//note that the function is returning an integer value
}



Well, I hope you got an idea as to how a return_type for a function works.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Error: Void Function

Posted 09 October 2012 - 01:22 PM

Couple of things I liked to mention out:
First, you don't really need to write a separate printf() to print an escape character, you can put it in the same e.g.
printf("1.Add\n");
.

Then you have declared choice as an array of 2 chars, as I think, perhaps, you might have thought that one would store y and the other n. But you really can declare only one char for choice and compare it directly without using any string functions like strcmp().
e.g.
 char choice;
           if (choice == 'y')
           else if (choice == 'n')



Another thing that jimblumberg has mentioned that you cannot define a function inside another. Well in your main() you put a declaration of function:
12	void menu_y(void);
. And as of your switch it seems that you want to take input from user and calculate it in the function. So, you can pass an argument to this function stating the option and implement this function. You can have your menu_y() function prototype maybe like this:
void menu_y(int op);
...

Hope this Helps!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1