C Programming - Functions - Discussion

5. 

Will the following functions work?

int f1(int a, int b)
{
    return ( f2(20) );
}
int f2(int a)
{
    return (a*a);
}

[A]. Yes
[B]. No

Answer: Option A

Explanation:

Yes, It will return the value 20*20 = 400

Example:


#include <stdio.h>
int f1(int, int); /* Function prototype */
int f2(int); /* Function prototype */

int main()
{
    int a = 2, b = 3, c;
    c = f1(a, b);
    printf("c = %d\n", c);
    return 0;
}

int f1(int a, int b)
{
    return ( f2(20) );
}

int f2(int a)
{
    return (a * a);
}

Output:
c = 400


Allam said: (May 8, 2014)  
But here how could function f1 know the exist of f2 cause f2 is defined after f1 ?

Neha said: (Dec 10, 2014)  
Can somebody explain it?

Ankit said: (Jan 2, 2015)  
No, because we don't the value of variable a.

Sankaran said: (May 24, 2016)  
@Ankit.

It assigns the value.

Priyanka said: (Nov 18, 2016)  
Please explain it.

Anonymous said: (Aug 22, 2017)  
Please explain it.

Manjusha said: (Aug 29, 2017)  
Explain it please.

Al_Nazre said: (Oct 18, 2017)  
A function cannot be defined inside another function but can called inside another function. This is what happens here.

Amit Kumar said: (May 12, 2018)  
Intially f1 function accept the value of variable a and b, again function f2 pas a value of a=20;again f2 function aperation a*a=20*20=400.

Shruthi said: (Nov 15, 2018)  
But Why a & b values are initialised? there is no usage of those.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.