4 votes 4 votes What will be the output of the following C program? #include <stdio.h> int main() { int f1(int,int); int x = 9,n = 3; printf("%d", f1(x, n)); } int f1(int x, int n) { int y = 1,i = 1; for(i = 1;i <= n; i++) y = y * x; return(y); } 27 729 81 Compilation Error Programming in C go-programming-1 programming programming-in-c + – Arjun asked Oct 18, 2016 Arjun 1.3k views answer comment Share Follow See all 0 reply Please log in or register to add a comment.
Best answer 3 votes 3 votes x = 9, n = 3; int f1(int x, int n) { int y = 1,i = 1; for(i = 1;i <= n; i++) y = y * x; return(y); } i = 1, y = 1, x = 9, n = 3, y = 1*9 = 9 i = 2, y = 9, x = 9, n = 3, y = 9*9 = 81 i = 3, y = 81, x = 9, n = 3, y = 81*9 = 729 return (y) i.e. 729 Digvijay Pandey answered Oct 20, 2016 • selected Oct 20, 2016 by Arjun Digvijay Pandey comment Share Follow See all 9 Comments See all 9 9 Comments reply mohit chawla commented Oct 26, 2016 reply Follow Share But their is no one to catch the value of function as function is returning integer? pls explain. 0 votes 0 votes Arjun commented Oct 26, 2016 reply Follow Share It is caught as it is used as an argument to printf. Also, even if it is not caught- there is no issue. Even printf always returns a value, and we rarely catch it. 3 votes 3 votes iamhusayn commented Dec 15, 2016 reply Follow Share The function f1(int,int) is defined inside main(). But it is supposed to be declared outside main(). So the code given in the question should generate a compilation error, right? 0 votes 0 votes Arjun commented Dec 15, 2016 reply Follow Share it is supposed to be declared outside main() This supposed to be is wrong. There is no requirement that a function declaration must be in the global space. Only issue of doing it inside another function is that, then the declaration is valid only for that function. 6 votes 6 votes Shubhi Tiwari commented Dec 30, 2016 reply Follow Share Kindly clarify @Arjun sir, So we can declare functions inside functions but can not define a function inside another function in C ,right? 2 votes 2 votes air1 commented Dec 30, 2016 i edited by air1 Jul 1, 2017 reply Follow Share you can define, declare, call functions inside functions in C. EDIT: nested function declarations are part of the C standard but nested definitions are not. 0 votes 0 votes Hemant Parihar commented Jul 1, 2017 reply Follow Share @air1 I first declare the function inside the main function. Then after some code. I define that function inside the main function. I got this error: "error: a function-definition is not allowed here before '{' token {" 1 votes 1 votes air1 commented Jul 1, 2017 reply Follow Share @Hemant Thanks for pointing. Just checked that nested functions are not a part of the C standard. They are just supported by GCC. You can get rid of that error by prepending the `auto` to the declaration. 1 votes 1 votes Shubhm commented Jul 9, 2019 i edited by Shubhm Jul 14, 2019 reply Follow Share function declaration inside main(). http://qa.geeksforgeeks.org/2839/qa.geeksforgeeks.org/2839/declaration-of-a-function-inside-main.html 0 votes 0 votes Please log in or register to add a comment.
0 votes 0 votes 1st time y=y*x; y=1*9=9; 2nd time loop will run y=y*x=9*9=81; 3rd time loop will run and again y=y*x=81*9=729; and next time condition will be false because i!<=n so y will return as o/p hence ans. is 729. SHIVAM GARG answered Jul 25, 2018 SHIVAM GARG comment Share Follow See all 0 reply Please log in or register to add a comment.