Sunday, 6 July 2014

Functions (Part1)

Functions (C-FAQ's)



Q1: Can you find out the error in the following code?

            func(int iA, int iB)

            {

                        int iA;

                        iA=40;

                        return iA;

            }

            int main()

            {

                        printf(" func = %d\n ", func(5,10) );

                        return 0;

            }

 

            A.        Missing parentheses in return statement

            B.        Redeclaration of iA

            C.        func should be defined as int func(int iA, int iB)

            D.        None of the above

 

           


            Output:        
                                B.        Redeclaration of iA.
        In function 'func':
        Line 3: error: 'iA' redeclared as different kind of symbol
        Line 1: error: previous definition of 'iA' was here
           


Q2: Can we use two return statement in a function ?

         


            Output:        
                        Yes we can you two return statements in a function.
                        Example:-
                                    int iMax ( int iA, int iB)
                                    {
                                                if ( iA > iB )
                                                            return  iA ;
                                                else
                                                            return iB;
                                    }
                       

Q3: Can we use two successive return statement in a function?

         


            Output:
                        No we can't use two successive return statements in a function.
                        Example :-
                                    int iMax()
                                    {
                                                return 100;
                                                return 200;
                                    }
                        Compiler will return an error.

Q4: What is the output of the following code?


            int func (int iNum)

            {

                        return (iNum++);

            }

            int main()

            {

                   int iNum= func(10);

                        printf(" iNum = %d \n ", --iNum);

                        return 0;

            }

 

            A.        iNum = 10

            B.        iNum = 9

            C.        iNum = 11

            D.        iNum = 8

               

           


            Output:
      iNum = 9 




<< PREV               [C FAQ's]            NEXT >>