Search code examples
c++return-valuefunction-prototypes

Return Value with Function Prototype


The problem that I am having is that VS (visual studio) gives me error C4715 'functionadd': must return a value. I understand what the compiler is trying to tell me; however, I don't know how to fix it. I am simply trying to get more familiar with function prototyping!!! Lastly, if someone can also show me how to prototype the struct it would also be much appreciated.

Main.cpp

#include "pch.h"
#include <iostream>
#include <string>
#include "func.h"



enum class myenum {
    NUMBERONE,
    NUMBERTWO,
    NUMBERTHREE,
    NUMBERFOUR,
    NUMBERFIVE,
};

struct mystruct{
    int age = 9;
    int willbeage;
    int avg;
    std::string about;
    std::string lastname;
} mystruct1;

int main()


{
    std::cout << "Hello World!\n";
    return 0;
}

func.h

#pragma once
#ifndef FUNC_H
#define FUNC_H
#include "pch.h"
int functionadd(int, int, int) {
}
void functionadd() {
}

int functionadd(int, int, int, int) {
}
#endif

func.cpp

#pragma once
#include "pch.h"
#include <iostream>
int functionadd(int a, int b, int c) {
    return a + b + c;
}
void functionadd() {
    std::cout << "Hello";
}

int functionadd(int a, int b, int c, int d) {
    return a + b + c + d;
}

Solution

  • You header file contains definitions for functions, not function prototypes. Get rid of the {} characters and terminate your prototypes with ;.

    #pragma once
    #ifndef FUNC_H
    #define FUNC_H
    #include "pch.h"
    int functionadd(int, int, int);
    void functionadd();
    int functionadd(int, int, int, int);
    #endif