Challenge - 5 Problems
Function Calling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested function calls
What is the output of this C program when run?
C
#include <stdio.h> int add(int a, int b) { return a + b; } int multiply(int x, int y) { return x * y; } int main() { int result = multiply(add(2, 3), add(4, 1)); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
Remember to evaluate the inner add() calls before multiply().
✗ Incorrect
The add(2, 3) returns 5, add(4, 1) returns 5, then multiply(5, 5) returns 25.
❓ Predict Output
intermediate2:00remaining
Function call with pointer argument
What will be printed by this C program?
C
#include <stdio.h> void update(int *p) { *p = *p + 10; } int main() { int val = 5; update(&val); printf("%d\n", val); return 0; }
Attempts:
2 left
💡 Hint
The function changes the value at the pointer's address.
✗ Incorrect
The function adds 10 to the value pointed by p, so val becomes 15.
❓ Predict Output
advanced2:00remaining
Output of recursive function call
What is the output of this C program?
C
#include <stdio.h> int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); } int main() { printf("%d\n", factorial(4)); return 0; }
Attempts:
2 left
💡 Hint
Factorial of 4 is 4*3*2*1.
✗ Incorrect
factorial(4) = 4*factorial(3) = 4*3*2*1 = 24.
❓ Predict Output
advanced2:00remaining
Output with function pointer call
What does this C program print?
C
#include <stdio.h> int square(int x) { return x * x; } int main() { int (*func_ptr)(int) = square; int val = 3; printf("%d\n", func_ptr(val)); return 0; }
Attempts:
2 left
💡 Hint
Function pointers can be called like normal functions.
✗ Incorrect
func_ptr points to square, so func_ptr(3) returns 9.
❓ Predict Output
expert2:00remaining
Output of function call with side effects and order
What is the output of this C program?
C
#include <stdio.h> int f(int *x) { (*x)++; return *x; } int main() { int a = 5; int result = f(&a) + f(&a); printf("%d\n", result); return 0; }
Attempts:
2 left
💡 Hint
The order of evaluation of function arguments is unspecified in C.
✗ Incorrect
Depending on order, f(&a) increments a twice. If left call evaluated first: f(&a) increments a to 6 returns 6, second call increments to 7 returns 7, sum 13.