Login


Lost your password?

Don't have an account ? Register ×
  
CPP All : Functions Reference Passing/Returning
Concept Learning Code Sheets

Check if string is a palindrome using a function. #1395

Check if a character array or c-style string is same (Palindrome) when reversed using an independent function.

#include <iostream>
#include <string.h>
using namespace std;
bool IsPalindrome(char* str)
{
int len=strlen(str);
for(int i=0;i<len/2;i++)
if(str[i] != str[len-i-1])
return false;
return true;
}
int main()
{
char input[40];
cout<<"Give a string input: ";
cin.getline(input,40);
if(IsPalindrome(input))
cout<<"Given input is a palindrome"<<endl;
else
cout<<"Given input is not a palindrome"<<endl;
return 0;
}

Call by reference demonstration #2647

A simple call by reference demonstration by adding something in the original value itself.

#include <iostream>
using namespace std;
void addsomething(int &);
int something=5;
int main()
{
int a=10;
cout<<"Before function call: a = "<<a<<endl;
addsomething(a);
cout<<"After function call: a = "<<a<<endl;
return 0;
}

void addsomething(int &x)
{
x=x+something;
}

Function Call by Reference and Call by Value comparison #2680

A program that uses both call by reference and call by value in the same program to compare it for better understanding.

#include <iostream>
using namespace std;
void increment(int &);
int increase_and_return(int);
int main()
{
int a=10;
cout<<"Before referenced function call: a = "<<a<<endl;
increment(a);
cout<<"After referenced function call: a = "<<a<<endl;
int b=increase_and_return(a);
cout<<"Using call by value: a = "<<a<<endl;
cout<<"Using call by value: b = "<<b<<endl;
return 0;
}

void increment(int &x)
{
++x;
}

int increase_and_return(int x)
{
return ++x;
}

Variable swapping using function call by reference #2685

Swapping (Interchange) of two variable values using the call by reference technique of function arguments.

#include<iostream>
using namespace std;
void swap(int &,int &);
int main ()
{
int a=10,b=13;
cout <<"The original values are : ";
cout <<" a = "<<a<<", b = "<<b<<endl;
swap (a,b);
cout <<"nThe values after swap () are : ";
cout <<" a = "<<a<<", b = "<<b<<endl;
return 0 ;
}
void swap(int &x,int &y)
{
int temp ;
temp = x ;
x = y ;
y = temp ;
}

Global and Local Function Declaration #2689

Understanding global and local function declaration using a function declaration above main and another inside the main routine.

#include <iostream>
using namespace std;
int action1(int,int);
int main()
{
int action2(int,int);
int a=10,b=15;
cout<<action2(a,b)<<endl;
cout<<action1(a,b)<<endl;
return 0;
}

int action1(int x,int y)
{
//int n=action2(x,y);
return x*y;
}

int action2(int x,int y)
{
return x+y;
}

Passing null terminated string to function using array notation #2701

Program to pass by reference character array as a null terminated string to a function using the array notation using [ ].

//using array notation
#include <iostream>
#include <string.h>
using namespace std;
int space_count(char []);
int main()
{
char str[80];
int spaces=0;
cout<<"Input a small sentence: " ;
cin.getline(str,80);
spaces=space_count(str);
cout<<"Number of spaces in given input = "
<<spaces<<endl;
return 0;
}

int space_count(char str[])
{
int sp_count=0;
int len=strlen(str);
for(int i=0;i<len;i++)
if(str[i]==' ') sp_count++;
return sp_count;
}

Passing null terminated string to function using pointer notation #2706

Program to pass by reference character array as a null terminated string to a function using the pointer notation using *.

//using array notation
#include <iostream>
#include <string.h>
using namespace std;
int space_count(char *);
int main()
{
char str[80];
int spaces=0;
cout<<"Input a small sentence: " ;
cin.getline(str,80);
spaces=space_count(str);
cout<<"Number of spaces in given input = "
<<spaces<<endl;
return 0;
}

int space_count(char *str)
{
int sp_count=0;
int len=strlen(str);
for(int i=0;i<len;i++)
if(str[i]==' ') sp_count++;
return sp_count;
}

Passing integer array to a function for linear search #2733

Program to pass an integer array, length of array and the member to be found to a function and return the position of the member found.

#include <iostream>
using namespace std;
int pos_in_arr(int [],int,int);
int main()
{
int arr[]={19,20,34,55,99,102,12};
int len=sizeof(arr)/sizeof(arr[0]);
int n;
cout<<"Find position of which number? " ;
cin>>n;
int pos=pos_in_arr(arr,len,n);
if(pos>0)
cout<<"Position of given input = "<<pos<<endl;
else
cout<<"not found"<<endl;
return 0;
}

int pos_in_arr(int array[],int len,int num)
{
for(int i=0;i<len;i++)
{
if(array[i]==num) return i+1;
}
return 0;
}

Changing multiple values of integer array passed to a function #2737

A program to pass integer array to a function and change many values in it based on a condition. Then reading such values in the calling function.

#include <iostream>
using namespace std;
int even2odd(int [],int);
int disp_arr(int [],int);
int main()
{
int arr[]={19,20,34,55,99,102,12};
int len=sizeof(arr)/sizeof(arr[0]);
cout<<"-- Array members before change -- "<<endl;
disp_arr(arr,len);
even2odd(arr,len);
cout<<"-- Array members after change -- "<<endl;
disp_arr(arr,len);
return 0;
}

int even2odd(int array[],int len)
{
for(int i=0;i<len;i++)
{
if(array[i]%2==0) array[i]+=1;
}
return 0;
}

int disp_arr(int array[],int len)
{
for(int i=0;i<len;i++)
{
cout<<array[i]<<" ";
}
cout<<endl;
return 0;
}


Function returning by reference #2749

A program to demonstrate returning of a reference data type from the function.

#include <iostream>
using namespace std;
int& setg();
int g=10;
int main()
{
cout<<"Global var g before change = "<<g<<endl;
setg()=20;
cout<<"Global var g after function call = "<<g<<endl;
return 0;
}

int& setg()
{
return g;
}

Using function return by reference to modify a global array #2754

A program that neatly modifies a global array by using the function return by reference technique.

#include <iostream>
using namespace std;
int& modifyga(int);
int ga[]={10,20,30,40,50};
int main()
{
for(int i=0;i<5;++i)
modifyga(i)=ga[i]+1;
for(int i=0;i<5;++i)
cout<<ga[i]<<" ";
return 0;
}

int& modifyga(int i)
{
return ga[i];
}

Solved Problems

output writing, pass by ref/pass by value #2652

Write the output of the following code piece.

Char array passing to function #2656

Write a function alpha_count() where a character array is passed as reference

Output writing – passing string as char pointer #2659

Write output of the following code.

Array to array data #2697

Write output of the following code and explain its working.

Output writing function within function with array passing #2746

Dry run the following program carefully and write the output.

Correcting error in function call by reference #2742

Program given below has some incorrectly written function call, correct the m

Setting bigger of two values using function returning reference #2757

Write a c++ program using function returning the reference which finds the b

First and last digit of an integer in a function and putting at reference location #3018

Write a c++ program using a function¬†with the following prototype –


Code Sheets:11  Solved Problems:8 
Back