#include<iostream.h>
int tripeCallByValue(int count1)

void main(){
int count=2;
cout<<"count is "<<count<<endl;
cout<<"count1 is "<<count1;
}
int tripeCallByValue(int count1){
count1=count*3;
return count1;
}

when i compile with compiler,i got a syntax error.but i didnt where i have did worng.

Dani AI

Generated

Quick diagnosis: the compile errors come from simple syntax and scope problems plus a bit of naming/semantics confusion. correctly spotted the prototype/return-type issue and poor formatting; and identified the assignment and scope mistakes; the error messages about “undefined symbol 'count1'” and “undefined symbol 'count'” mean a name was referenced outside its defining scope (or misspelled).

Concrete checklist to make the exercise correct and robust

  • Give the prototype a terminating semicolon and keep the function name consistent (typos like tripe vs triple cause link/compile problems).
  • Pass count into the function and operate on that parameter (call-by-value gives the function a copy).
  • “Triple” means multiply by 3 (not aaa).
  • Use standard headers and a proper main signature (modern compilers expect #include <iostream> and int main()).

Example (clean, modern C++ showing call-by-value vs call-by-reference):

#include <iostream>

int tripleByValue(int x) { return x * 3; }
int tripleByReference(int &x) { x *= 3; return x; }

int main() {
    int count = 2;
    std::cout << "count = " << count << '\n';
    std::cout << "tripled (by value) = " << tripleByValue(count) << '\n';
    std::cout << "count after by-value call = " << count << '\n';
    std::cout << "tripled (by reference) = " << tripleByReference(count) << '\n';
    std::cout << "count after by-reference call = " << count << '\n';
    return 0;
}

Expected behaviour for count = 2: the call-by-value result is 6 but count remains 2; the call-by-reference changes count to 6. Final tips: enable compiler warnings (e.g. g++ -std=c++17 -Wall -Wextra) and fix the first syntax error the compiler reports — many subsequent errors cascade from that initial problem. This ties together the corrections suggested by , and and resolves the semantic confusion raised by .

Recommended Answers

All 11 Replies

line 10 should be count1 = count1 * 3

You're also missing a ';' after your prototype on Line 2. Also, while you're at it, you should change your main() to return-type int and add a return 0; at the end of the function.

This is where the whitespace lecture occurs.

Make better use of whitespace and your code will be easier to read. As written/posted, your code is a jumbled mess, I'm not surprised you can't see it.

Reformatted for readability demo (syntax errors not corrected):

#include<iostream.h>

int tripeCallByValue(int count1)

void main() {
  int count = 2;
  cout << "count is " << count << endl;
  cout << "count1 is " << count1;
}

int tripeCallByValue(int count1) {
  count1 = count * 3;
  return count1;
}

Do you see how much easier it is to see the elements of your code/syntax (operators, variables, etc.) when compared to your original code?

still got error.T^T,help pls.gonna crazy soon.

Hello sing1006,
You haven't initialized count1 in the main() and count can be used only in the main() (local scope).

Hello sing1006,
You haven't initialized count1 in the main() and count can be used only in the main() (local scope).

The question is like not sure im doing right or wrong.
-Function tripleCallByValue that pass a copy of count call-by-value, triples the copy and return the new value.
In the question above,triples the copy means 2*2*2 or 2*3?
pls,help begging u all.2moro the due date for this exercise.

Can you post the errors you got ?

Can you post the errors you got ?

Error E2451 x.cpp 7: Undefined symbol 'count1' in function main()
Error E2451 x.cpp 10: Undefined symbol 'count' in function tripeCallByValue(int)

That's what i said in my previous post. Count is bound to main() only you cannot use it elsewhere and count1 can be used in the tripecallbyvalue() function only. So you should modify the lines 7 and 8 like this..

cout<<"count is "<<count<<endl;
cout<< "count1 is " << tripeCallByValue(count);

and line 10 in the function tripeCallByValue() like this...

count1 = count1 * 3;

That's what i said in my previous post. Count is bound to main() only you cannot use it elsewhere and count1 can be used in the tripecallbyvalue() function only. So you should modify the lines 7 and 8 like this..

cout<<"count is "<<count<<endl;
cout<<"count1 is "<<tripeCallByValue(count);

and line 10 in the function tripeCallByValue() like this...

count1 = count1 * 3;

erm,i think i got it.
but can u help mee check is that the answer match to the question?
Function tripleCallByValue that pass a copy of count call-by-value, triples the copy and return the new value.

#include<iostream.h>
int tripleCallByValue(int);
void main(){
int count=2;
cout<<"count="<<count<<endl;
cout<<"Value returned by tripleCallByValue:"<<tripleCallByValue(count)<<endl;
}
int tripleCallByValue(int a){
return a*a*a;
}

It's not a*a*a i think. Triple means a * 3.

The result of [TEX]a*a*a[/TEX] is not a triple it's a cube [TEX](a^3)[/TEX], there is a huge difference. A triple is multiplication by the number 3.

Using 2 as an example value for 'a':
[TEX]2*2*2 = 2^3 = 8[/TEX] (this is a cube)
[TEX]2 * 3 = 6[/TEX] (this is a triple)

Using 10 as an example value:
[TEX]10*10*10 = 10^3 = 1,000[/TEX] (this is a cube)
[TEX]10 * 3 = 30[/TEX] (this is a triple)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.