-2

I am wrote a stack example but it shows a error message when compiled.

In switch case 4: exit (); this line is the problem : think

64  9   C:\Users\pavilion 15\OneDrive\Documents\stack.cpp   [Error] return-statement with a value, in function returning 'void' [-fpermissive]

This is the error message I saw when compiling this code. Can some one please help me to solve this?

#include<stdio.h>
#include<conio.h>
#define MAX 5

int stack[MAX];
int top = -1;

void push(int);
int pop();
void display();

main(){

    int choice,num;
    while(1){
        printf("Enter your choice\n");
        printf("1.Push\n");
        printf("2.Pop\n");
        printf("3.Display\n");
        printf("Exit\n");
        scanf("%d", &choice);

        switch(choice){
            case 1:
                printf("Enter a number");
                scanf("%d", &num);
                push(num);
                break;
            case 2:
                 num = pop();
                 break;
            case 3:
                display();
                break;
            case 4:
                exit(1);
            default:
                printf("invalid input");
        }
    }

}


void push(int element){

    if(top == MAX-1 ){
        printf("Stack OverFlow");
        return;
    }
    top = top +1;
    stack[top]=element;
}

void pop(){
    int element;
    if(top == -1){
        printf("Stack is empty\n");
        return;
    }
    element = stack[top];
    top = top - 1;
    printf("%d has been deleted", element);
    return element;
}

void display(){
    int i;
    if(top==-1){
        printf("stack is empty!");
        return;
    }
    printf("\n\n");
    for(i=top;i>0;i--)
    printf("%d\n", stack[i]);
}

enter image description here

1

3 Answers 3

2

You declared int pop() but you defined void pop(), and inside the definition you try to return element.

Now this would normally lead to an error when you do num = pop() since it wouldn't be able to find the correct definition for the function you forward declared but the compiler stops earlier since the function void pop() is not well formed.

0
0

You cannot return a value from a void function.

void pop(){
    int element;
    if(top == -1){
        printf("Stack is empty\n");
        return;
    }
    element = stack[top];
    top = top - 1;
    printf("%d has been deleted", element);
    return element;
}

needs to be int pop()

etc.

1
0

You can't return an element from pop() - it's void (no return type)...

... Exactly like the error message says!

The easiest fix is to make it return an int, but then you need a special return code to indicate an error. Alternatively: int pop(int *v) - returns 0 on failure or 1 on success, and populates v.

1
  • Read the whole answer: once you make pop return an int you have to address your error case (currently just return;) - YoIf you say your are returning an int from a function then you always have to return an int from it.
    – John3136
    Sep 24, 2015 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.