Home › Functions › Quiz (Functions)
Quiz (Functions) Test your understanding of return values, pass-by-value, void functions, and prototype declarations.
Question 1 — Return value
int add (int a, int b) {
return a + b;
}
int main (void ) {
printf ("%d\n" , add (3 , 7 ));
return 0 ;
}
What does this print?
7
10
3
Compile error
Explanation: add(3, 7) receives a=3 and b=7, and returns 3 + 7 = 10.
Question 2 — Pass by value
void change (int x) {
x = 100 ;
}
int main (void ) {
int a = 5 ;
change (a);
printf ("a = %d\n" , a);
return 0 ;
}
What does this print?
a = 100
a = 5
a = 0
Compile error
Explanation: C uses pass-by-value . Inside change, x is a copy of a. Modifying x has no effect on the original a, so it stays 5 .
Question 3 — void function
void greet (void ) {
printf ("Hello!\n" );
return 1 ;
}
What happens?
Compiles fine
Compile error (or warning)
Runtime error
Prints "Hello!" and 1
Explanation: A void function can't return a value, so return 1; is a compile error . Inside a void function you can only use return; with no value.
Question 4 — Declaration and call order
#include <stdio.h>
int main (void ) {
printf ("%d\n" , square (5 ));
return 0 ;
}
int square (int n) {
return n * n;
}
What happens?
25
Compile error (implicit declaration)
0
5
Explanation: square is defined after main. Without a prototype declaration , the compiler doesn't yet know about square when it's called, which produces an implicit-declaration warning or error. Fix: add int square(int n); before main, or move the function's definition above main.
Question 5 — Scope of local variables
void foo (void ) {
int x = 10 ;
}
int main (void ) {
foo ();
printf ("%d\n" , x);
return 0 ;
}
What happens?
Prints 10
Compile error (x is out of scope)
Prints 0
Runtime error
Explanation: A
local variable's scope is limited to the block where it was declared .
x, declared inside foo, is only valid inside foo
When foo returns, x stops existing
Since x is not visible from main, the compiler reports an "undeclared" error
To share values between functions, use
parameters ,
return values , or global variables.
Question 6 — Recursive function calls
int fact (int n) {
if (n <= 1 ) return 1 ;
return n * fact (n - 1 );
}
int main (void ) {
printf ("%d\n" , fact (4 ));
return 0 ;
}
What does this print?
16
24
4
Infinite loop
Explanation: A classic
recursive function that computes a factorial.
fact(4) = 4 * fact(3)
fact(3) = 3 * fact(2)
fact(2) = 2 * fact(1)
fact(1) = 1 (base case)
Result: 4 * 3 * 2 * 1 = 24
Every recursive function needs a
base case . Without one, it will run forever and eventually overflow the stack.
Question 7 — Multiple arguments and order
int sub (int a, int b) {
return a - b;
}
int main (void ) {
printf ("%d\n" , sub (3 , 10 ));
return 0 ;
}
What does this print?
7
-7
13
Compile error
Explanation: Arguments are bound
in the order they're declared .
Call: sub(3, 10)
Inside the function: a=3, b=10
a - b = 3 - 10 = -7
Mixing up argument order is a common source of bugs. Pick an order that matches what the function is supposed to do.
Question 8 — Early return
int check (int n) {
if (n < 0 ) return -1 ;
if (n == 0 ) return 0 ;
return 1 ;
}
int main (void ) {
printf ("%d\n" , check (-5 ));
return 0 ;
}
What does this print?
0
-1
1
-5
Explanation: When a
return statement runs, the function
exits immediately .
n = -5, so n < 0 is true
return -1; runs
Everything after that is skipped
"Early return" is popular for guarding against errors and edge cases — it keeps nesting shallow.
Question 9 — Argument type conversion
int twice (int x) {
return x * 2 ;
}
int main (void ) {
double d = 3.7 ;
printf ("%d\n" , twice (d));
return 0 ;
}
What does this print?
7.4
6
Compile error
8
Explanation: The parameter type is
int, so the
double argument is
implicitly converted to int (truncated) .
d = 3.7 → converted to int → 3 (the fractional part is dropped, not rounded)
twice(3) = 3 * 2 = 6
The compiler may warn about the implicit conversion. If it's intentional, make it explicit with a cast like
(int)d.
Result Answer all the questions to see your score.