Click here to Skip to main content
16,004,587 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I was trying to create a function and got an error that "too few arguments to functions"
the code is given below:

C++
#include<iostream>
using namespace std;

int sum(int a ,int b){
    int c=a+b;
    return c;
}

int main(){
    int d;
    int e;
    cout<<"enter num 1"<<endl;
    cin>>d;
    cout<<"enter num 2"<<endl;
    cin>>e;
    cout<<"the sum is"<<sum(d+e);
    return 0;
}



The error was like this:

PS E:\code practice> cd "e:\code practice\" ; if ($?) { g++ tutt9.cpp -o tutt9 } ; if ($?) { .\tutt9 }
tutt9.cpp: In function 'int main()':
tutt9.cpp:16:32: error: too few arguments to function 'int sum(int, int)'
   16 |     cout<<"the sum is"<<sum(d+e);
      |                                ^
tutt9.cpp:4:5: note: declared here
    4 | int sum(int a ,int b){
      |     ^~~
PS E:\code practice>


What I have tried:

Tried changing the project file but the same result every time.
Posted
Updated 22-Oct-20 7:21am
v2

Look at your code, not the project file. Your definition of the sum function is:
C++
int sum(int a ,int b){

So it requires two integers as parameters. but when you call it at:
C++
sum(d+e);

You only pass one value, the sum of d and e.
 
Share this answer
 
Comments
CPallini 22-Oct-20 12:34pm    
5.
Richard MacCutchan 22-Oct-20 13:37pm    
Thank you. I got the language right this time. :)
CPallini 22-Oct-20 14:50pm    
This is a beloved language.
You separate params with a comma

sum(d, e)


If you don't know how to call a function I strongly suggest you spend more time learning the basics of the language before trying to write anything.
 
Share this answer
 
Comments
CPallini 22-Oct-20 12:35pm    
5.
Your function
C++
int sum(int a ,int b){

wants 2 arguments a and b,
but you call it as
C++
sum(d+e);

which combine d and e into 1 argument.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900