In: Computer Science
Array & Function - Can I pass a single parameter to a function that takes two arguments:
data passed from:
arrayOne(4);
to function:
string function (array[], int)
***************Answer with Explnation***************
Yes, you can pass 1 arguement to a function taking two parameters by using default arguement in function parameter...
But there some rules to be followed when using default arguement
1. Once default value is used for an argument in function definition, all subsequent arguments(the arguements defined after the default arguement) to it must have default value.....or the default arguement parameter should be the last parameter in function definition
2.Default arguement value is overwritten when value for that parameter is passed..
3. When calling the function..arguements from calling function to called function are copied from left to right.For example, if function call is total(5,10,15) and function definition is int total(int a,int b,int c,int d=0) then it will assign 5,10,15 to a,b,c respectively and d will be =0 bcoz of default arguement
****************Summary***************
But as given in question you are passing arrayOne(4)..i.e 4 is an integer value ..that means you want to pass the value for the second parameter in function (array[],int) and define array[] as the default parameter...but we cannot do that (1st rule )because it is not the last parameter in function defintion...so you'll have to change sequence of parameter
Assuming that you don't want to give the pass the arguement for array[] parameter...I have given a program below in c++ to explain what exactly you'll have to do...
I hope it works for you :) !!!!
Ask for help in comments if needed
**************Program**************
#include<iostream>
using namespace std;
char arr[]={'H','i','t','h','e','r','e'}; //intializing char array..
//can't intialize default values of array directly in braces..
//you can give int,float,string default values in braces
string arrayOne(int n,char array[]=arr)
//n is normal paremeter and array[] is default arguement
{
char str[n]; //array with size n
for(int i=0;i<n;i++) //for loop to store store only part of array to str
{
str[i]=array[i];
}
return str; //return str
}
int main()
{
int number;
char array1[10];
cout<<"Enter the string you want to trim"<<endl;
cin>>array1; //taking array from user
cout<<"How much of the string do you wanna keep"<<endl;
cin>>number; //taking value for n in function call
//calling function arrayOne() and printing string
//here the array is overwrittem by user array becoz we have passed two arguements
cout<<"The trimmed string is "<<arrayOne(number,array1)<<endl<<endl;
cout<<"How much of the default string do you wanna keep"<<endl;
cin>>number; //taking value for n in function
//calling function arrayOne() and printing trimmed default string
//here we have not passed 2nd arguement so it will take the default value of array specified in function defintion
cout<<"The trimmed string is "<<arrayOne(number);
return 0;
}
**************Output****************
Program Screenshot