How to add function in C++, Visual Studio Community 2022

Renjie Zhang 40 Reputation points
2024-03-17T08:57:13.01+00:00

How to add function in C++, Visual Studio Community 2022.

System: Windows 11 Home

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,720 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Minxin Yu 11,586 Reputation points Microsoft Vendor
    2024-03-18T03:01:46.5866667+00:00

    Hi, @Renjie Zhang

    Functions (C++)

    Provide a function declaration in the header(.h) file and provide a definition of the function in the associated .cpp file.

    declaration :

    //Sum.h
    int sum(int a, int b);
    

    definition :

    //Sum.cpp
    #include"Sum.h"
    int sum(int a, int b)
    {
        return a + b;
    }
    

    Use the function in ConsoleApplication.cpp

    #include<iostream>
    #include"Sum.h"// include the header file
    
    int main()
    {
        int i = sum(10, 32);
        int j = sum(i, 66);
        std::cout << "The value of j is" << j << std::endl; // 108
    }
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Renjie Zhang 40 Reputation points
    2024-03-21T09:59:06.74+00:00

    Dear Minxin,

    The example you provided is for adding a method in the console, not in Windows.

    In the console, we only need to define the function type and add variables; in the window, we also need to add resources. The system we applied is VS C++, not VC++.

    The below case is for console, not for Windows.

    So would you kindly let me know how to add functions in Windows, C++, Visual Studio Community 2022?

    Regards,

    Renjie

    User's image

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.