How to Create a C++ Function

Understand the function syntax., Start with a program without a user defined function., Run the output., Write CPP program with a function., Run the output with the function.

5 Steps 1 min read Medium

Step-by-Step Guide

  1. Step 1: Understand the function syntax.

    Before call a function, first declare it using void.

    After declaring a function starfunction, define arguments on it using function body.

    When defining of a function is completed. call it anywhere using its name and a semicolon. like: starfunction();.
  2. Step 2: Start with a program without a user defined function.

    Write these line of codes in your C++ IDE.

    This isn't totally necessary, but to help you learn, start with a program without a user defined function and run it. #include<iostream> #include<conio.h> using namespace std; int main () { std::cout<<"Data Type Range"<<endl; std::cout<<"Char
    -128 to 127"<<endl; std::cout<<"Short
    -32,768 to 32,767"<<endl; std::cout<<"Int System dependent"<<endl; std::cout<<"Long
    -2,147,483,648 to 2,147,483,647"<<endl; getch(); } , It will give you ranges of different data types.

    Now you can add a user defined function starfunction in it. , Write these line of codes in you C++ IDE, compile the code, and run it. #include<iostream> #include<conio.h> using namespace std; void starfunction (); //Function Declaration int main () { starfunction(); //Function Call std::cout<<"Data Type Range"<<endl; starfunction(); //Function Call std::cout<<"Char
    -128 to 127"<<endl; std::cout<<"Short
    -32,768 to 32,767"<<endl; std::cout<<"Int System dependent"<<endl; std::cout<<"Long
    -2,147,483,648 to 2,147,483,647"<<endl; starfunction(); //Function Call getch(); } void starfunction() // Function Declator { for (int a=1; a<=27; a++) std::cout<<'*'; std::cout<<endl; } At the bottom of the code, in starfunction we define a for loop and print 27 (*) stars.

    When we call starfunction function anywhere else, without typing for loop it prints 27 (*) stars. , It will give a new look to your program.
  3. Step 3: Run the output.

  4. Step 4: Write CPP program with a function.

  5. Step 5: Run the output with the function.

Detailed Guide

Before call a function, first declare it using void.

After declaring a function starfunction, define arguments on it using function body.

When defining of a function is completed. call it anywhere using its name and a semicolon. like: starfunction();.

Write these line of codes in your C++ IDE.

This isn't totally necessary, but to help you learn, start with a program without a user defined function and run it. #include<iostream> #include<conio.h> using namespace std; int main () { std::cout<<"Data Type Range"<<endl; std::cout<<"Char
-128 to 127"<<endl; std::cout<<"Short
-32,768 to 32,767"<<endl; std::cout<<"Int System dependent"<<endl; std::cout<<"Long
-2,147,483,648 to 2,147,483,647"<<endl; getch(); } , It will give you ranges of different data types.

Now you can add a user defined function starfunction in it. , Write these line of codes in you C++ IDE, compile the code, and run it. #include<iostream> #include<conio.h> using namespace std; void starfunction (); //Function Declaration int main () { starfunction(); //Function Call std::cout<<"Data Type Range"<<endl; starfunction(); //Function Call std::cout<<"Char
-128 to 127"<<endl; std::cout<<"Short
-32,768 to 32,767"<<endl; std::cout<<"Int System dependent"<<endl; std::cout<<"Long
-2,147,483,648 to 2,147,483,647"<<endl; starfunction(); //Function Call getch(); } void starfunction() // Function Declator { for (int a=1; a<=27; a++) std::cout<<'*'; std::cout<<endl; } At the bottom of the code, in starfunction we define a for loop and print 27 (*) stars.

When we call starfunction function anywhere else, without typing for loop it prints 27 (*) stars. , It will give a new look to your program.

About the Author

B

Brittany Anderson

Creates helpful guides on crafts to inspire and educate readers.

38 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: