finctions?

This is a discussion on finctions? within the C++ Programming forums, part of the General Programming Boards category; i dont really get what a function is and what i do with it? so far i think its a ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    47

    finctions?

    i dont really get what a function is and what i do with it?
    so far i think its a way of staring a prosses
    EX:
    like i can make a + 5 function and call it add
    and now i can type that in and it will do the finction
    well i dont think thats what it really dose and the tutoreal dosnt help me so if you cold post how to use it and what is dose...
    mabe a working example?

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    a function is basicly a piece of code that can be executed many times within the program for example:
    Code:
    #include <iostream>
    using namespace std;
    
    add5 (int num)
    {
    return num + 5;
    int main()
    {
    int num1, num2;
    cout << "Enter a number: ";
    cin >> num1;
    num2 = add5(num1);
    cout<< num1 << " + 5  = " << num2 << endl;
    
    cout << "Enter a number: ";
    cin>> num1;
    num2 = add5(num1);           // Function is reused
    cout << num1  << " + 5 = " << num2 << endl;
    system("PAUSE");
    return 0;
    See the function gets reused sometimes the functions get really big so it can save you lots of coding and it can improve the performance of the progrm

  3. #3
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    its sort of like a mini program in your program. if you've ever used basic then its kinda like using gosub. calling a function just tells the program to go to the program name and execute the code there.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    47
    i stil dont know how to make a function
    when i said example i ment like:
    Code:
    while ( the condition )
    {
    the function
    }

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    A function is, like everybody else said, a separate piece of code. You can tell the computer to run this piece of code whenever you like. You can have it produce a result (call the return value) and you can give it information to use (parameters).

    Here's a simple example:
    Code:
    int add(int first, int second)
    {
    	/* code to do anything you want */
    	return first + second;
    }
    The first "int" is the type that you're returning - an integer. The word "add" is the name of the function. Inside the parentheses is a comma delimited list of parameters that you're expecting and the names by which you'll call them. Inside the brackets is the code to execute. You use the return keyword to return a value of the type you specified at the beginning. If you do not wish to return anything, you sepcify, "void", as the return type. If you don't want to accept any parameters, just put nothing in between the parentheses.

    Beyond that, you need to go and read a tutorial.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi, compare the following two programs which do the exact same thing.

    Using functions:
    Code:
    #include <iostream>
    using namespace std;
    
    void displayStuff(int num)
    {
    	cout<<"The multiplication table for "<<num<<" is: "<<endl;
    	for(int i = 1; i <= 12; i++)
    	{
    		cout<<num * i<<" ";
    	}
    	cout<<endl;
    
    	cout<<"Counting down from "<<num<<" to 0 is:"<<endl;
    	for(i = num; i >=0; i--)
    	{
    		cout<<i<<" ";
    	}
    	cout<<endl;
    
    	cout<<"Counting from "<<num<<" to 10 is:"<<endl;
    	if(num < 10)
    	{
    		for(i = num; i <= 10; i++)
    		{
    			cout<<i<<" ";
    		}
    	}
    	else
    	{
    		cout<<num<<" is greater than or equal to 10."<<endl;
    	}
    	cout<<endl<<endl;
    
    }
    
    int main ()
    {
    	displayStuff(4);
    	displayStuff(8);
    	displayStuff(12);
    
    	return 0;
    }
    No functions:
    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	
    	int num = 4;
    	cout<<"The multiplication table for "<<num<<" is: "<<endl;
    	for(int i = 1; i <= 12; i++)
    	{
    		cout<<num * i<<" ";
    	}
    	cout<<endl;
    
    	cout<<"Counting down from "<<num<<" to 0 is:"<<endl;
    	for(i = num; i >=0; i--)
    	{
    		cout<<i<<" ";
    	}
    	cout<<endl;
    
    	cout<<"Counting from "<<num<<" to 10 is:"<<endl;
    	if(num < 10)
    	{
    		for(i = num; i <= 10; i++)
    		{
    			cout<<i<<" ";
    		}
    	}
    	else
    	{
    		cout<<num<<" is larger greater than or equal to 10."<<endl;
    	}
    	cout<<endl<<endl;
    
    	num = 8;
    	cout<<"The multiplication table for "<<num<<" is: "<<endl;
    	for(i = 1; i <= 12; i++)
    	{
    		cout<<num * i<<" ";
    	}
    	cout<<endl;
    
    	cout<<"Counting down from "<<num<<" to 0 is:"<<endl;
    	for(i = num; i >=0; i--)
    	{
    		cout<<i<<" ";
    	}
    	cout<<endl;
    
    	cout<<"Counting from "<<num<<" to 10 is:"<<endl;
    	if(num < 10)
    	{
    		for(i = num; i <= 10; i++)
    		{
    			cout<<i<<" ";
    		}
    	}
    	else
    	{
    		cout<<num<<" is larger greater than or equal to 10."<<endl;
    	}
    	cout<<endl<<endl;
    
    	num = 12;
    	cout<<"The multiplication table for "<<num<<" is: "<<endl;
    	for(i = 1; i <= 12; i++)
    	{
    		cout<<num * i<<" ";
    	}
    	cout<<endl;
    
    	cout<<"Counting down from "<<num<<" to 0 is:"<<endl;
    	for(i = num; i >=0; i--)
    	{
    		cout<<i<<" ";
    	}
    	cout<<endl;
    
    	cout<<"Counting from "<<num<<" to 10 is:"<<endl;
    	if(num < 10)
    	{
    		for(i = num; i <= 10; i++)
    		{
    			cout<<i<<" ";
    		}
    	}
    	else
    	{
    		cout<<num<<" is larger greater than or equal to 10."<<endl;
    	}
    	cout<<endl<<endl;
    
    	return 0;
    }
    Which one is easier to read and figure out what it does?
    Last edited by 7stud; 06-16-2005 at 12:35 AM.

  7. #7
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Well when you don't know how to use functions, the one that doesn't use functions is easier to read and figure out

  8. #8
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    The example 7stud gave is a great example
    Just remember that functions are mostly used to save time and coding. If there is something you going to be doing alot instead of rewriting the code all over just make a functions that will do what ever you need it to do.

    You can call a functions from functions also:
    Code:
    #include <iostream>
    using namespace std;
    
    int Multiply(int num1, int num2)
    {
    	//Other code if needed
    	return num1 * num2;
    }
    int Add(int num1, int num2)
    {
    	return num1 + num2;
    }
    void Choice(int choice)
    {
    	int num1 , num2;
    	if(choice == 1)
    	{
    		cout << "Input Numbers: ";
    		cin >> num1 >> num2;
    		cout << num1 << " * " << num2 << " = " << Multiply(num1, num2) << endl;
    	}
    	if(choice == 2)
    	{
    		cout << "Input Numbers: ";
    		cin >> num1 >> num2;
    		cout << num1 << " + " << num2 << " = " << Add(num1, num2) << endl;
    	}
    }
    int main()
    {
    	int choice;
    	cout << "What would you like to do." << endl;
    	cout << "1. Multiply" << endl;
    	cout << "2. Add" << endl;
    	cin >> choice;
    	Choice(choice);
    
    	system("PAUSE");
    	return 0;
    }
    main is also a functions... its a integer function that returns an integer...
    Last edited by mrafcho001; 06-15-2005 at 08:18 PM.

  9. #9
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    firefly... read this post.. it's much more clear, and progressive
    http://cboard.cprogramming.com/showt...164#post413164

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    47

    hmm

    Ok i think im getting it
    so far i can put a equation in the
    Code:
    { }
    and i think in the title i put int
    Code:
    function name ( no idea what this is for )
    { 
    x++
    if ( x == 5 ) 
    {
    break;
    };
    else ()
    {
    cout<<"hello";
    };
    };
    ok that was my idea of a function i think
    in the no idea bow i think its something like the part in red

    ex:
    Code:
    if ( x <= 10 )
    something like that am i getting it right yet?

  11. #11
    Deo
    Deo is offline
    Registered User
    Join Date
    May 2005
    Posts
    73
    Things in parenthesis aren't functions.

    Code:
    int main()
    {
      int x = 3;
    
     if(x == 5)
       cout << "x is equal to 5";
     else
       cout << "x does not equal 5";
    }
    (x==5) is a conditional statement.. If this is true then run the next line of code. Otherwise x does not equal 5 so run the statement after the else statement.

    Code:
    bool isEqual(int variable)
    {
      if(variable == 5)
         return true;
     else
       return false;
    }
    Code:
    int main()
    {
      int x = 3;
    
      bool result;
    
      result = isEqual(x);
       
      if(result == true)
       cout << "x is equal to 5";
     else
       cout << "x does not equal 5";
    }
    The first thing my function will do is return true or false. bool.
    The function is called isEqual you can call it whatever you want. The function takes in an int paremeter and renames it variable.

    So when my main program calls isEqual(x) The function takes x and represents it as variable. Function performs on this parameter passed in then returns the result.

    Function is code independent and reusable.

    You can call isEqual as many times in your main program without having to rename it.

    ex:
    Code:
    int main()
    {
      int x = 3;
      int y = 5;
    
      bool result;
    
      result = isEqual(x);  // Pas in x this time x = 3;
       
      if(result == true)
       cout << "x is equal to 5";
     else
       cout << "x does not equal 5"; 
    
      result = isEqual(y);  // Pass in y this time.  y = 5
       
      if(result == true)
       cout << "y is equal to 5";
     else
       cout << "y does not equal 5"; 
    }
    Functions are not equations.. They just perform a task. The can be used to display output, gather input, draw a character to a screen, make a character move, etc..

    Code:
    int main()
    {
    x=x+5;
    y=5+x;
    
    if (y > 10)
    }
    The above has nothing to do with functions and no functions need to be defined.
    Last edited by Deo; 06-17-2005 at 02:00 PM.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    I suggest you not worry about functions at this point. You don't know enough about the basics yet. In my beginning C++ book, functions aren't introduced until chapter 8. It looks to me like you would be on about chapter 2 or 3.

  13. #13
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Maybe this will example will help:
    Code:
    #include <iostream>
    using namespace std;
    
    void show(int num)
    {
    	cout<<num<<endl;
    }
    
    int main()
    {
    	int num = 10;
    	cout<<num<<endl;
    
    	//or with a function:
    	
    	num = 30;
    	show(num);
    
    	return 0;
    }
    Last edited by 7stud; 06-17-2005 at 04:16 PM.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Here is a very simple function.
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber()
    {
       cout<<100<<endl;
    }
    
    int main ()
    {
       showNumber();
    
       return 0;
    }
    Functions must specify a 'return type', and the return type precedes the function name. In the function above, there is no return value, and that is designated by 'void'. The function is 'called' in main() by this line:

    showNumber();

    That says, "please go find a function named showNumber, and execute the lines of code contained therein."

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    void showNumber(int a)
    {
       cout<<a<<endl;
    }
    
    int main ()
    {
       showNumber(100);
       showNumber(25);
    
       return 0;
    }
    Now, instead of always displaying the same number, you can send the function a specific number, and the function will display whatever number you send it. Once again, the function does not return a value, so its return type is 'void'. You should note that the type of the number you send to the function must match the function 'parameter'. The function parameter above is "int a", which means the function is expecting an int type to be 'sent' to it. You send a value to a function like this:

    showNumber(100);

    To send a value to a function, you just put the value between the parentheses after the function name. The function parameter "int a" creates an integer variable named "a" which stores the value you send to the function--that's why the type of the value you send has to match the function parameter. Then, inside the function you can use the variable "a" by referring to it by name:

    cout<<a<<endl;

    Here is another example:
    Code:
    #include <iostream>
    using namespace std;
    
    int addTen(int a)
    {
       int answer = a + 10;
       return answer;
    }
    
    int main ()
    {
       int result1 = addTen(2);
       int result2 = addTen(5);
    
       cout<<"Here are the results: "<<result1<<" "<<result2<<endl;
       
       return 0;
    }
    This time the function returns a value. The return type preceding the function name must match the type of the value in the 'return statement'. The return statement replaces the function call in main(), e.g. addTen(2), with the return value. So, result1 is assigned 12, and result2 is assigned 15.

    To summarize:
    1) A function must list a return type before its name.
    2) The return type must match the type of the value in the return statement--if there is no return statement, then the type is 'void'.
    3) When you call a function, the value you send it, must match the function parameter.
    4) The function call in main() is replaced by the function return value.
    5) Sometimes if a function doesn't have any parameters, like in the first example, "void" will be used for the parameter. For instance you might see the function in the first example written like this:
    Code:
    void showNumber(void)
    {
       cout<<100<<endl;
    }

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21