Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Create and invoke a function that returns a value. Show an example of how th

ID: 3661655 • Letter: C

Question

C++

Create and invoke a function that returns a value. Show an example of how this is done.

Create and invoke a function that does not return a value. This called a void function. Show an example of a void function and how it is called from the main program.

What is passing by value vs by reference. Show examples.

What is a function prototype. Show an example of a function prototype

Define what we mean by a variable’s scope and lifetime. Show an example to help you define this concept.

What is a one dimensional array. Show an example of declaring a one dimensional array.

Give an example of how you get data into a one dimensional array from the keyboard.  

Give an example of how to display data from #7 above.

Give an example of how to pass a one-dimensional array to a function

Define parallel one-dimensional arrays and show an example that demonstarate the use of parallel arrays.

What is a twodimensional array. Show an example of declaring a two dimensional array.

Give an example of how you get data into a two dimensional array from the keyboard.  

Give an example of how to display data from #12 above.

Explanation / Answer

1. Function that retunrs a value and sample exaple program
#include<iostream>
using namespace std;
//square function prototype
int square(int n);
//starting point of program execution
int main()
{

   int n=10;
   //call square function
   int result=square(n);
   //print result
   cout <<"Square value of "<<n<<" is "<<result<endl;  
   //pause the programme output
   system("pause");
   //returns 0
   return 0;
}


//A square function square that takes an integer n and
//returns square of the value n
int square(int n)
{

int square=n*n;
//return a square value
return square;

}
------------------------------------------------------------------------------------------
2. Function that dost not retunrn any value and sample exaple program
#include<iostream>
using namespace std;
//square function prototype
void square(int n);
//starting point of program execution
int main()
{

   int n=10;
   //call square function
   square(n);  
   //pause the programme output
   system("pause");
   //returns 0
   return 0;
}


//A square function square that takes an integer n and
//prints the value of square of n
void square(int n)
{

int square=n*n;
//print result
cout <<"Square value of "<<n<<" is "<<result<endl;  

}

------------------------------------------------------------------------------------------
3.
Passing argumets to functions is of two types
Pass by value : The passing arguments to the method will take a copy of

the values passed to the
function. Changes to the arguments inside the function will not reflect

outside of the function.
The arguments becomes the local variables of the function.

Pass by reference :
The passing arguments to the method will take a reference value that is

address of the values passed to the
function. Changes to the arguments inside the function will be in effect

even outside of the function.
The argumets passed to the function by pass by reference are global in

effect.

swapping of two values using pass by value and pass by reference

//swappig pass by value
void swap(int x, int y)
{
   int temp=x;
   x=y;
   y=temp;
}

//swappig pass by reference
void swap(int *x, int *y)
{
   int temp=*x;
   *x=*y;
   *y=temp;
}
------------------------------------------------------------------------------------------
4.Function prototype
Function :
Declaration of function before main metod writing its defintion in the

program is called as function protoype.

#include<iostream>
using namespace std;

//function prototype
return_type functionName(arg1, arg2, arg3, ...argn);

int main()
{
//code follows
}
------------------------------------------------------------------------------------------
5.Scope of variables and lift time

The scope of a variable in a function is only limited to inside the block
or method . The variable cannot be accessed by outside of the function.
------------------------------------------------------------------------------------------
6. One-dimensional array:
The one dimensional array is array of fixed size contigous memory
locations.
Declaration of array
dataType variableName[size];

//example of one dimensional array of size=10 of integer type
int arr[10];

------------------------------------------------------------------------------------------
7.Reading data from One-dimensional array

int arr[10];
for(int index=0;index<10;index++)
   //read element from keyboard and store in arr at index positon
   cin>>arr[index]

------------------------------------------------------------------------------------------
8.Display data of one-dimensional array to console

int arr[10];
//reading
for(int index=0;index<10;index++)
//read element from keyboard and store in arr at index positon
cin>>arr[index]

//displaying one dimensional array to console
for(int index=0;index<10;index++)
   //write element to console
   cout<<arr[index]
------------------------------------------------------------------------------------------
9. Passing array to a function

void display(int arr[])
{
//displaying one dimensional array to console
for(int index=0;index<10;index++)
   cout<<arr[index]
}
//To pass an array of one dimesional array to display function
//calling display method with one dimensional array
display(arr);
------------------------------------------------------------------------------------------
10. Parallel one-dimensional arrays
Parallel arrays of same size defined to read values of different
data types

//Parallel arrays to read name, socres and grades
char names[50];
int scores[5];
float grade[5];

------------------------------------------------------------------------------------------
11.Declare two dimensional array

A 2-D dimensional array is array of rows and columns to store elements
in a table like structure.

//Declaration of 2D dimensional array to store
//a check board
int board[8][8];

------------------------------------------------------------------------------------------
12. Reading values into two dimensional array
//Declaration of 2D dimensional array to store
//a check board
int board[8][8];

for(int row=0;row<8;row++)
for(int col=0;col<8;col++)
cin>>board[row][col];

------------------------------------------------------------------------------------------
13. Display values from 2D array

for(int row=0;row<8;row++)
for(int col=0;col<8;col++)
cout<<board[row][col];