function examples in c++

Image result for c++In C scope of variable means after declaring/defining a variable where can we read value stored in that variable. If we have defined a variable inside any function then that variable can be read after defining it anywhere in that function, but not outside. If variable is defined inside loop then it can not be read outside loop. It is easy to understand. Variable defined inside any {...} can be read anywhere inside that after defining, but not outside.
In the example given below we will make a register where any name can be added and all names in register can be printed. When you run this program, a menu will be displayed which will give you option to add any name in register or print all names.
#include <stdio.h>
char
list[10][20];
int length = 0;
void add_name() {
if
(length >= 10) {
printf("list is full\n");
return
;
}

printf("Enter the name: ");
scanf("%s", list[length]);
length = length + 1;
printf("name added\n");
}

void print_list() {
int
i = 0;
for
(i=0; i < length; i++) {
int serial_no = i + 1;
printf("%d : %s\n", serial_no, list[i]);
}
}

int main() {
int
input = 0;
while
(input != 3) {
if(input == 0) {
printf("0 - Print this menu\n");
printf("1 - add name to list\n");
printf("2 - print list\n");
printf("3 - quit program\n");
}

else if
(input == 1) {
add_name();
}

else if
(input == 2) {
print_list();
}

else {
printf("Wrong Input, try again\n");
}

scanf("%d", &input);
}
}

Running program given above will print a menu. Enter 1 to add a name, Then it will ask what name you want to enter. Type the desired name and enter. To enter another name Again enter 1. To print all names you have entered press 2. Try to rum this program 1st then we will understand how it works.

Now lets see how it works. In beginning we have defined 2 variables: list and length. Since these 2 variables are not inside any function or { } hence can be accessed from anywhere in program.
Now lets see char list[10][20]; just consider char list[10]; 1st which means array of characters with capacity 10. char list[10][20]; means it can contain 10 arrays with each array have capacity of 20 char. Here instead of 10 chars it will contains 10 arrays.
If it was int a[2][3] then we can see in the way show below.
a = {  {1,2,3} ,   {4,5,6}   };

Here a can hold 2 arrays one at a[0] and another at a[1]. Each of these two arr can hold 3 integers.
We will use char list[10][20]; to store names. We have seen that name is a string which can be stored in char array. Here list can contain 10 arrays hence we can store 10 names. Each array can hold up to 20 char Hence we can store names having less than 20 char. 2nd variable length how many names have been added to the list which is 0 in the beginning since no name on the list.
After that there is add_name function, calling which adds name into the list. 1st we check if list is full(10 names added) then stop executing function using return statement, otherwise read name in the list using scanf. 1st argument of scanf is %s hence it will read string, hence 2nd argument should be char array. list[0], list[1], list[2] ... list[19] are char array.
After adding name to the list length is increased by 1. list[length] always gives next empty space in list. In the beginning list is empty hence list[length] (length=0) is empty. after adding a name at list[0] length becomes 1 now list[1] (length = 1) is empty.
After that print_list function is defined, which reads length variable to know how many named are on the list. It prints all those names using for loop.Inside loop a variable serial_no is defined which can not be accessed outside for loop.

In the end there is main function from where execution starts. Using if-else according to input variable either we add name to the list, or print list or print menu inside a while loop. While loop will continue till user enters 3.
If you like this, share this with you friends in facebook, twitter,orkut.