+ Reply to Thread
Results 1 to 7 of 7

Thread: Functions

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Functions

    Functions

    A function is a way of encapsulating code for reuse. The proper use of functions can make modifying your code much easier.

    A Basic Function

    Code:
    void out(int v) {
    	v++;
    	cout << v << endl;
    }
    You call this function like this:

    Code:
    out(6);
    Output:

    7
    Now, whenever you want to output an integer. All you need to do is call the out function and pass the integer.

    When you declare a function you use this syntax:

    return type name(parameters) {
    body
    }

    Arrays by Reference? Passing Arrays

    When you pass parameters they are passed by copy. However, arrays and objects are a little different. When you pass an array to a function, you pass the memory address of the array to the function. This is how it appears to be passed by reference.


    Passing by reference

    Code:
    void update(int& v) {
     	v++;
    }
    
    int main() {
    	int v = 5;
    	update(v);
    	cout << v << endl;
    
    	return 0;
    }
    Output:

    6

    When you want things updated in the function, passing by reference is useful. You might want this behaviour when you are sorting arrays.

    Example:

    Code:
    void sort(int v[], int size) {
    	for (int i=0;i<size-1;i++) {
    		for (int x=i+1;x<size;x++) {
    			if (v[x] < v[i]) {
    				v[x] ^= v[i];
    				v[i] ^= v[x];
    			}
    		}
    	}
    }
    
    int arn[] = {4,3,2,5,1,7};
    
    sort(arn,6);
    
        for (int i=0;i<6;i++) {
            cout << arn[i] << " ";
        }
        cout << endl;
    Even though, the function is not indicating the array is being passed by reference, it is being passed by a reference as you can tell when you look at the output. The array has been sorted.

    Output:

    1 2 3 4 5 7
    Constant parameters

    Passing a value as a constant ensures that you can never modify the variable in the function.

    This might be useful in a search function.

    Example:

    Code:
    int search(const int v[], int size, int target) {
        for (int i=0;i<size;i++) {
            if (v[i] == target) {
                    return i;
            }
        }
        return -1;
    }
    This code returns i which is the index of the target in the array or -1 if it is not found.

    If I try to modify v in the function, I get an error saying trying to modify a read only parameter.

    Return values

    All methods return a value which is indicated with a return statement. There may be several return statements in a function but a function can onyl return 1 value. Unless the function is a void function in which case it does not return any value.

    Recursive functions

    A function that calls itself is said to be a recursive function. These are complicated beasts and are a topic for their own tutorial.

    Example:

    Code:
    int fib(int n) {
            if (n <= 1) return n;
            return fib(n-1) + fib(n-2);
    }
    Last edited by chili5; 08-28-2009 at 05:29 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Functions

    When you pass parameters they are passed by copy except with arrays, and objects. Arrays and objects are passed b y revference which means that if they are modified in the function then they are modified outside of the function. Otherwise, the variable is passed by copy and changes made in the function are not made outside of the function.
    Sorry, chili, but this isn't accurate.
    All variables are passed as a local copy unless explicitly passed by reference. This includes objects and arrays. However (!) when passing an array into a function, you actually use the name of the array which is a pointer. That, combined with the fact that use can use the [] operator with pointers makes it appear that you've passed it by reference.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Functions



    Didn't know that, I was assuming that they were passed by reference.

  5. #4
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Functions

    Does this make more sense?

    When you pass parameters they are passed by copy. However, arrays and objects are a little different. When you pass an array to a function, you pass the memory address of the array to the function. This is how it appears to be passed by reference.

  6. #5
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Functions

    More importantly, it's accurate Typically, large objects are passed by reference or a pointer to the object is passed. It takes a conscious choice, however.

    Don't worry about it too much, it's those little things that can be "gotcha" moments for C/C++
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    Join Date
    Nov 2009
    Posts
    20
    Rep Power
    0

    Re: Functions

    well written thx

  8. #7
    Phineas is offline Newbie
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Re: Functions

    Tyvm for making this.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. using functions.
    By apejam in forum Python
    Replies: 2
    Last Post: 10-25-2011, 09:13 PM
  2. Replies: 4
    Last Post: 02-06-2011, 01:37 PM
  3. c# and api functions
    By Computer Engineer in forum C# Programming
    Replies: 6
    Last Post: 05-28-2010, 06:56 AM
  4. SQL Functions - SQL Encryption Functions
    By chili5 in forum Tutorials
    Replies: 8
    Last Post: 09-04-2009, 09:40 AM
  5. SQL Functions - Math Functions
    By chili5 in forum Tutorials
    Replies: 6
    Last Post: 09-02-2009, 02:11 PM

Tags for this Thread

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