Exforsys

Online Training

Passing Arguments to Functions

This is a discussion on Passing Arguments to Functions within the C and C++ forums, part of the Programming Talk category; I am trying different ways of passing arguments to functions in C++. I wrote the below program which passes arguments ...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-29-2007, 11:33 AM
Senior Member
 
Join Date: Apr 2006
Posts: 144
caradoc is on a distinguished road
Passing Arguments to Functions

I am trying different ways of passing arguments to functions in C++. I wrote the below program which passes arguments to function by Passing a pointer by value.

void sample(int *t);
main()
{
int x;
int *p;
x = 10;
p = &x;
sample(p);
cout << "x is %d" << x << " \n";
}

void sample(int *i)
{
*i = *i + 1;
}

If I want to code the same function by using the concept of passing by reference how can I do that. Kindly guide me on the same.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-02-2007, 02:14 AM
Senior Member
 
Join Date: Apr 2006
Posts: 125
ashlee is on a distinguished road
You can code the same function by using the concept of passing by reference in C++ as below:
void sample(int &t);
main()
{
int x;
x = 10;
sample(x);
cout << "x is %d" << x << " \n";
}

void sample(int &x)
{
x = x + 1;
}

In the above changes made to the reference passed in function sample will also be made to the original variable as it is passed by reference.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 06-15-2004 07:00 AM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 06-01-2004 07:01 AM
comp.lang.c Answers to Frequently Asked Questions (FAQ List) Steve Summit Tech FAQ 0 06-01-2004 07:00 AM
comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ) Steve Summit Tech FAQ 0 05-15-2004 07:00 AM
comp.cad.autocad AutoLISP FAQ (part 1/2) - general Reini Urban Tech FAQ 0 05-01-2004 06:30 AM


All times are GMT -4. The time now is 05:04 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.