🇯🇵 日本語 | 🇺🇸 English
Advertisement

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, 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 does not affect the original a, so it remains 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 cannot return a value. return 1; is a compile error.
A void function can only have return; (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 know about square when it's called, producing an implicit-declaration warning or error.
Fix: declare int square(int n); before main, or place the function before main.

Result

Please answer the questions
Home