Results 1 to 1 of 1
  1. #1
    1337/PWNR
    Programmer
    Emu's Avatar
    Join Date
    Aug 2010
    Location
    Virginia
    Posts
    161
    Points
    292
         User Info     Contact     Gamer ID
    Join Date
    Aug 2010
    Location
    Virginia
    Posts
    161
    Points
    292
    Gamer IDs

    Gamertag: SpidermaN EaT PSN ID: Shaq23Jordan

    Contact info:

    Youtube Channel: www.youtube.com/eemuohg Kik Username: ShaqGlizzy

    Default programming with emu: L3; c++ functions & returns

    in this tut i will explain a few things about functions and value returning functions and their differences.

    firstly every program has a main function if this function is not found by the compiler it will not know where to start.
    Code:
    int main(void)
    {
       return 0;
    }
    the main function is a value returning function as on can see because of the int beside the function's identifier named main and also the return 0 where 0 is an int being return.

    from here on out are code snippets then at the end will be a full program for one to experiment with.

    functions that return:
    functions that return require a data type such as int, double, bool, char and so on.
    lets write a value returning function and call it
    Code:
    int add_fnc(void)
    {
       int a, b, c; //initialize multiple variables, identified as a, b, and c of data type integer
       
       a=1; //a gets 1
       b=2; //b gets 2
    
       c=a+b; //c gets a+b, which will be 3 because 1+2=3
    
       return c; //return c
    }
    YOU MUST RETURN THE DATA TYPE THAT THE FUNCTION IS, such as int add_fnc(void)...

    main:
    Code:
    int main(void)
    {
       //we can call the add_fnc two way, way 1 is below
       int a=add_fnc(); //since the function returns a value of type int this is legal, and will assign the value 3 to a
       cout<<a<<endl;
    
       //way 2
       cout<<add_fnc()<<endl; //this is legal because the function returns a value, which will output 3
    
       system("pause");
       return 0;
    }
    value returning functions are useful for checks and keeping your code neater by separating functions

    here is the full code here:
    Code:
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    int add_fnc(void)
    {
       int a, b, c; //initialize multiple variables, identified as a, b, and c of data type integer
       
       a=1; //a gets 1
       b=2; //b gets 2
    
       c=a+b; //c gets a+b, which will be 3 because 1+2=3
    
       return c; //return c
    }
    
    int main(void)
    {
       //we can call the add_fnc two way, way 1 is below
       int a=add_fnc(); //since the function returns a value of type int this is legal, and will assign the value 3 to a
       cout<<a<<endl;
    
       //way 2
       cout<<add_fnc()<<endl;
    
       system("pause");
       return 0;
    }
    note: YOU MUST PUT FUNCTIONS ABOVE THE MAIN FUNCTION UNLESS YOU HAVE FUNCTION PROTOTYPES, I WILL BE EXPLAINING PROTOTYPES AND PARAMETERS NEXT TUTORIAL.

    0 Not allowed! Not allowed!
    Last edited by Emu; 03-02-2013 at 03:57 PM.

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -10. The time now is 03:00 PM.
Powered by vBulletin®
Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.