Subscribe to C++ FunSchool        RSS Feed
-----

[C++] Function Returning

Icon Leave Comment
/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
=======================================================
COPYRIGHT (c) 2013, Naveen Venkat | All Rights Reserved
=======================================================
THERE ARE SOME MUST READ GIVEN AT THE END OF THE PROGRAM. PLEASE DO READ THEM.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/

#include<iostream.h>
#include<string.h> 	//FOR strlen() function
#include<stdio.h>	  	//FOR gets() function

int func(char a[], int stringlength, char k)
{
	for (int i=0; i<stringlength; i++)
	{
		if (a[i]==k)
		{
			return(1);
			//It returns 'true', and ends** the function there itself, so the further statements do not execute
			//**return brings the control out of the function, equal to termination (end) of a function
		}
	}
	return(0);
	//If it did not end the function by returning 'true', this statement will return 'false'
}

void main()
{
	char a[40]; char m; int st; //a=string, m=charecter, st= string length
	cout << "ENTER YOUR STRING: ";
	gets(a); //As, if we write cin, it will end after the first space. So "being human" will be taken as "being" only!
	cout << "ENTER THE CHARECTER TO CHECK: ";
	cin >> m; //For a single charecter, cin is no problem. If we try to input "abc" here, it will take only "a".
	st = strlen(a); //For the end point of the loop which will compare charecter by charecter in the function.

	if (func(a,st,m)) //Here, if the function returns 1, then it will be if(1), [if(true)], which is true so it will display "String has m in it".
	{
		cout << "The string has " << m << " in it.";
	}
	else //If, the function returns 0, it will become if(0), [if(false)], which means execute the else statement.
	{
		cout << "The string does not have " << m << " in it.";
	}
}

/*
MUST READ:

-----------------------
RETURNING OF A FUNCTION
-----------------------

ALWAYS REMEMBER THAT THE FUNCTION ENDS ITS RUNNING (EXECUTION) AS SOON AS YOU WRITE return STATEMENT [in a non-void function]
A return STATEMENT GIVES THE VALUE TO THE STATEMENT IN WHICH THE FUNCTION IS CALLED.
For eg,

			int square(int a)
			{
				int b = a*a;
				return(a);
			}
					OR                         [BOTH DO THE SAME JOB]

			int square(int b)
			{
				return (b*b);
			}

will make the value of square(2) as 4.
So,
	cout << square(2);
will display 4 on the screen.

--------------------------------
true and false during comparison
--------------------------------

1) 			  value 0 => FALSE
		any other value => is TRUE.
		So even if you return(10) or return (-285) from the function, it will be considered true.

2)		Expression A --> if(func(a,st,m))
		Expression B --> if(func(a,st,m)==1)

		both the expressions (exp.) are same.

		As, if the function returns a true value (1), then exp. A becomes if(1) and exp. B becomes if(1==1).
		Now == is a comparison operator. If both are equal it makes the expression TRUE, ie. 1 (by default, 1 is taken as true).
		So, exp. B also becomes if(1).

		If the function returns a false value (0), then exp. A becomes if(0), and exp. B becomes if(0==1).
		Since, == will only make the expression 1 if both the sides are equal, so if(0==1) becomes if(0), which is same as exp. A

*/

0 Comments On This Entry

 

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

September 2014

S M T W T F S
 123456
78910111213
14151617 18 1920
21222324252627
282930    

Tags

    Recent Entries

    Search My Blog

    0 user(s) viewing

    0 Guests
    0 member(s)
    0 anonymous member(s)

    Categories