#include <iostream>
using namespace std;
int display(int a)
{
cout<<"enter a"<<endl;
cin>>a;
cout<<a<<" "<<a<<endl;
}
int display_three_times(int a)
{
cout<<a<<endl;
cout<<a<<endl;
cout<<a<<endl;
}
int main()
{
float a;
display(a);
display_three_times(a);
return 0;
}
By executing the program, I got something like this:
Results of the program.
So my question is, how to use the variables in the second subprogram while they were entered in the first subprogram? I know it's a really basic question, but I do need help :(
You need to pass the result of "display" into "display_three_times". You can do this by replacing
display(a);
display_three_times(a);
with
display_three_times(display(0));
and append return a; to the display function.
Some style tips. Why declare a as a float in main when you are only dealing with int? Since display doesn't take any input you could remove the parameter int a and declare it as a variable instead. display is perhaps misleadingly named since it also reads an integer. To avoid undefined behaviour also return an int from display_three_times or replace it with void; since we do not use the return value of display_three_times we may as well just change the return type to void. With these changes your program looks like:
#include <iostream>
using namespace std;
int read_and_display()
{
int a;
cout<<"enter a"<<endl;
cin>>a;
cout<<a<<" "<<a<<endl;
return a;
}
void display_three_times(int a)
{
cout<<a<<endl;
cout<<a<<endl;
cout<<a<<endl;
}
int main()
{
int a=read_and_display();
display_three_times(a);
return 0;
}
You can use pass by reference or you can return the value from the first function and pass it to another function
Simply passing the variable, means you are passing its value(here 0) only. On passing the variable a in display() you are passing its value only. And inside display() function it is other independent variable 'a' which receives the value. And any change is will be in valid in its scope only inside the block of that function.
To use the same variable in another block you can pass the variable as a refrence variable.
And you can not type cast as int when you are passing it as refrence variable, because it means you are passing the exactly same variable.
#include <iostream>
using namespace std;
int display(float &a)
{
cout<<"enter a"<<endl;
cin>>a;
cout<<a<<" "<<a<<endl;
return 0;
}
int display_three_times(float &a)
{
cout<<a<<endl;
cout<<a<<endl;
cout<<a<<endl;
return 0;
}
int main()
{
float a;
display(a);
display_three_times(a);
return 0;
}
Related
#include<iostream>
using namespace std;
void sum(int x,int y);
void sum(int a,int b)
{
int a,b;
int sum;
cout<<"Enter Two numbers "<<endl;
cin>>a>>b;
sum=a+b;
}
int main()
{
int z=Sum(a,b)
cout<<"Sum is "<<z<<endl;
}
Please tell me what I did wrong. It is giving me some errors for parameters.
There are several issues here:
You're attempting to call Sum which does not exist. You should be calling sum.
The values you're passing to the function do not exist. There are no variables called a or b at the point where the function is called.
The function is declared to not return a value (i.e. the return type is void) but you attempt to use the return value. The function should be changed to have a return type of int and you need to return sum.
The parameters a and b that sum expects are shadowed by local variables called a and b. This means the parameters are never used. Since you have nothing to pass in anyway, just remove the parameters.
After applying these fixes, your program will look like this:
#include<iostream>
using namespace std;
int sum();
int sum()
{
int a,b;
int sum;
cout<<"Enter Two numbers "<<endl;
cin>>a>>b;
sum=a+b;
return sum;
}
int main()
{
int z=sum();
cout<<"Sum is "<<z<<endl;
}
You can do it as follows and learn more here about adding functions.
#include <iostream>
using namespace std;
int sum(int, int);
int main()
{
int num1, num2, x;
cout<<"Enter two integer numbers: ";
cin>>num1>>num2;
//This will call the first function
cout<<"Result: "<<sum(num1, num2)<< endl;
return 0;
}
int sum(int a, int b)
{
return a+b;
}
because there is no return in your function.
if you want to save the result in a new variable you have to make a return 1)
or you just safe it as a already existing variable 2)
1)
int sum(int a,int b)
{
int a,b;
int sum;
cout<<"Enter Two numbers "<<endl;
cin>>a>>b;
sum=a+b;
return sum_ab;
}
void sum(int a,int b)
{
int a,b;
int sum;
cout<<"Enter Two numbers "<<endl;
cin>>a>>b;
a=a+b;
}
Here's what I have:
#include <iostream>
using namespace std;
void test(double &r)
{
r = 0.1;
}
int main() {
double rdefined;
double yo = test(&rdefined);
cout << yo <<endl;
return 0;
}
I've tried putting the test function after the main function and assigning rdefined as 0.0 .
The function declaration void test(double &r) specifies that the argument is passed by reference not as a pointer. To pass a "pointer-to-double" use: void test(double *r).
Although, in practice, the effect is very similar, when passing an argument by reference, you don't explicitly give the address; so, in your code, the call should be as shown below. Also, as noted in the answer given by Vettri, your function does not return a double value (or, indeed, anything, as it is void), so you can't assign the variable yo directly from the call to it.
Try this, as one possible solution:
test(rdefined); // Changes the value of "rdefined"
double yo = rdefined; // Assigns modified value to "yo"
For a discussion on the differences between pointers and references, see here.
Corrected Solution:
#include <iostream>
using namespace std;
double test(double* r)
{
*r = 0.1;
return *r;
}
int main() {
double rdefined;
double yo = test(&rdefined);
cout << yo <<endl;
return 0;
}
You need to specify the correct return value. You got an error because you are expecting a double value in return of your test function, but you declared it as void.
The only thing you need to do is, to replace & with * in the function parameters. here is the code.
#include <iostream>
using namespace std;
void test(double* r)
{
*r = 0.1;
}
int main() {
double rdefined;
test(&rdefined);
cout << rdefined <<endl;
return 0;
}
I just noticed something when creating functions. In the code:
#include <iostream>
using namespace std;
int add(int a, int b = 20)
{
int r;
r = a + b;
return (r);
}
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
it will work because the function being called is on top of the caller, but if I put the function add() below the calling function in main() it won't work.
#include <iostream>
using namespace std;
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
int add(int a, int b = 20)
{
int r;
r = a + b;
return (r);
}
and the compiler will tell me that the identifier add() cannot be found.
so why do we declare functions anyway? like this:
#include <iostream>
using namespace std;
int add(int a, int b = 20);
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
int add(int a, int b)
{
int r;
r = a + b;
return (r);
}
A definition is implicitly a declaration. And a declaration must come ahead of the use.
All functions need to be declared before they are used.
You can do that by either (1) writing a declaration, or (2) writing a definition.
Relying solely on (2) can be tempting but then you are bound to order your program in a particular way, and is occasionally impossible. For example, the following will not compile unless the comment is removed.
//void bar(int);
void foo(int n)
{
if (!n){
bar(n);
}
}
void bar(int n)
if (n){
foo(n);
}
}
int main()
{
foo(1);
}
No.
If the function definition appears before the function call, then prototype is not mandatory. Otherwise function prototype is necessary to let compiler know how to respond to a function when it is called.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
if the function definition appears after the function call then prototype is mandatory. because it tells the compiler to how to respond the function when it is called.
check the following example.
/* C++ Function Prototype and C++ Function Definition */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int add(int, int); // function prototype
void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
getch();
}
int add(int x, int y) // function definition
{
int res;
res = x + y;
return res;
}
and if the function definition is made before the function call then it is not mandatory to declare function prototype.
consider example.
/* C++ Function Prototype and C++ Function Definition */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int add(int x, int y) // function definition
{
int res;
res = x + y;
return res;
}
void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
getch();
}
in the following code for swapping by call by refernce is used
#include <iostream>
#include <conio.h>
using namespace std;
void swap(int& c,int& d)
{
int temp;
temp=c;
c=d;
d=temp;
}
int main()
{
int a,b;
cout<<" Enter value of a";
cin>>a;
cout<<"\n Enter value of b";
cin>>b;
swap(a,b);
cout<<"a: "<<a;
cout<<"\n b:"<<b;
return 0;
}
but if i use class and make object then there is no need of '&' refernce in calling the method.
#include <iostream>
#include <conio.h>
using namespace std;
class swapy
{
public:
int a;
int b;
void swap(int c,int d)
{
int temp;
temp=c;
c=d;
d=temp;
}
};
int main()
{
swapy s;
cout<<" Enter value of a";
cin>>s.a;
cout<<"\n Enter value of b";
cin>>s.b;
swap(s.a,s.b);
cout<<"a: "<<s.a;
cout<<"\n b:"<<s.b;
return 0;
}
Can you explain how this worked?Why & was not needed in the second program.
First of all you are not calling your class swap() function, because you are not calling it with your class object s, for class swap() function you have to call it with s. like
s.swap(c,d);
Further Change your function swap() name to any other name like mySwap() then you'll get to know that this is not correct, because swap() is a built-in function of std::
so it will be simple to understand if you change the name of your function.
Furthermore, you are not using class member variables correctly, you have to do something with your class member variables a and b. your class is useless here.
I am trying to pass a variable from one function to another. I have tried this approach but it is not working for me:
int c (){
int x1,x2,y2,y1;
system("cls");
cout<<"Insert Value"<<endl
cin>>x1;
return x1;
}
int cd()
{
int a;
a=c();
cout<<"X1: "<<a;
}
Any help is appreciated. Thanks!
There are a few problems with your code.
First of all you are missing a semicolon after the cout statement in your c() function.
Also you have indicated that function cd() should return an int but you are not returning anything.
Lastly, these function will not begin execution unless you call them explicitly.
Try this:
#include <iostream>
using namespace std;
int c (){
int x1,x2,y2,y1;
cout<<"Insert Value"<<endl;
cin>>x1;
return x1;
}
int cd(){
int a;
a=c();
cout<<"X1: "<<a;
return a;
}
int main()
{
int x=cd(); //call the function to create the side effects
return 0;
}