0
0
Cprogramming~20 mins

Function declaration and definition - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Function Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple function call
What is the output of this C program?
C
#include <stdio.h>

int add(int a, int b);

int main() {
    int result = add(3, 4);
    printf("%d\n", result);
    return 0;
}

int add(int a, int b) {
    return a + b;
}
A7
B34
C0
DCompilation error
Attempts:
2 left
💡 Hint
Look at what the function 'add' returns and what is printed.
Predict Output
intermediate
2:00remaining
Function declaration without definition
What happens when you compile and run this C code?
C
#include <stdio.h>

int multiply(int x, int y);

int main() {
    printf("%d\n", multiply(2, 3));
    return 0;
}
ALinker error
B0
C6
DCompilation error
Attempts:
2 left
💡 Hint
The function is declared but not defined anywhere.
🔧 Debug
advanced
2:00remaining
Identify the error in function definition
What error does this code produce?
C
#include <stdio.h>

int square(int n) {
    return n * n;
}

int main() {
    printf("%d\n", square(5));
    return 0;
}
AUndefined function 'square' (Linker error)
BNo error, outputs 25
CType mismatch error
DMissing semicolon after return statement (SyntaxError)
Attempts:
2 left
💡 Hint
Check punctuation at the end of the return line.
Predict Output
advanced
2:00remaining
Function with pointer parameter output
What is the output of this program?
C
#include <stdio.h>

void increment(int *p) {
    (*p)++;
}

int main() {
    int a = 10;
    increment(&a);
    printf("%d\n", a);
    return 0;
}
ACompilation error
B10
C11
DGarbage value
Attempts:
2 left
💡 Hint
The function changes the value at the address passed.
🧠 Conceptual
expert
3:00remaining
Function declaration and definition order
Which option correctly explains why this code compiles and runs without errors?
C
#include <stdio.h>

void greet();

int main() {
    greet();
    return 0;
}

void greet() {
    printf("Hello!\n");
}
ABecause the function is defined before main, so no declaration is needed.
BBecause the function is declared after main, so the compiler ignores the call.
CBecause the function is defined inside main, so it is local and accessible.
DBecause the function is declared before main and defined after, the compiler knows its signature when main calls it.
Attempts:
2 left
💡 Hint
Think about how the compiler processes declarations and definitions.