Beginner Function Calling Question

  • Thread starter Thread starter chronie
  • Start date Start date
  • Tags Tags
    Beginner Function
Click For Summary
SUMMARY

This discussion centers on function calling in C++ with a focus on how to print arguments passed to a function. The user, Mike, inquires about printing the parameters of the function matheq from main.cpp and whether he needs to redefine them in another file, func2.cpp. The solution involves placing the cout statements inside the matheq function, which successfully resolves the issue. This demonstrates the importance of understanding function scope and parameter handling in C++.

PREREQUISITES
  • Basic understanding of C++ syntax and structure
  • Knowledge of function definitions and calls in C++
  • Familiarity with header files and multiple source files in C++
  • Experience with the iostream library for input and output operations
NEXT STEPS
  • Explore C++ function overloading to manage multiple functions with the same name
  • Learn about passing parameters by reference and value in C++
  • Investigate the use of header files to declare functions for better code organization
  • Study debugging techniques in C++ to identify and fix common coding errors
USEFUL FOR

Beginner C++ programmers, computer science students, and anyone looking to improve their understanding of function calls and parameter management in C++.

chronie
Messages
23
Reaction score
0
Below is a sample code. My professor starts code and we have to finish the code. Below is not the assignment but a sample I have written up to help my question. I have two .cpp files one named main the other named func1. Within func1 I have a function called matheq. I was just wondering if there was some way to have my main.cpp print out the matheq numbers in the parentheses? Or do I have to redefine them in my second func2.cpp file?

I added a cout statement trying to show what I am trying to do, I know it is wrong but I just wanted to show some basic code to back up my statement.

Thanks again for your help!

-Mike
Code:
#include<iostream>
void matheq(double a,double b,double c);
using namespace std;
int main()
{

matheq(1.0,3.3,2.1);
matheq(1.0,3.3,5.1);
matheq(1.0,3.3,0.0);

cout << "matheq(a) << endl;}
 
Technology news on Phys.org
Before calling matheq(), you could have an output statement like this:
Code:
cout << "First arg: 1.0" << " Second arg: 3.3" << " Third arg: 2.1" << endl.
matheq(1.0, 3.3, 2.1);
// and so on

Is that what you're asking?
 
Yeah that is exactly what I was looking for. However, I think I have found my error. I believe I need to do my count statements inside of my matheq function. I did that and it worked.

Thank you though Mark for responding!
 

Similar threads

Replies
6
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
12
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
10K