Bird
Raised Fist0
Cprogramming~20 mins

Function prototypes - Practice Problems & Coding Challenges

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

int add(int a, int b); // function prototype

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

int add(int a, int b) {
    return a + b;
}
A0
BCompilation error
C34
D7
Attempts:
2 left
💡 Hint
Remember what the add function does and how prototypes help the compiler.
❓ Predict Output
intermediate
2:00remaining
Effect of missing function prototype
What error or output occurs when calling a function without a prototype in C99 or later?
C
#include <stdio.h>

int main() {
    int x = multiply(2, 3);
    printf("%d\n", x);
    return 0;
}

int multiply(int a, int b) {
    return a * b;
}
ACompilation error: implicit declaration of function 'multiply'
B6
C0
DRuntime error
Attempts:
2 left
💡 Hint
Check if the compiler knows about multiply before main.
🔧 Debug
advanced
2:00remaining
Identify the prototype mismatch error
Which option shows a prototype that will cause a mismatch error with the function definition below?
C
int compute(int x, int y);

int compute(int a, float b) {
    return a + (int)b;
}
Aint compute(float x, float y);
Bint compute(int x, float y);
Cint compute(int x, int y);
Dint compute(int, int);
Attempts:
2 left
💡 Hint
Compare parameter types in prototype and definition carefully.
🧠 Conceptual
advanced
1:30remaining
Purpose of function prototypes
What is the main purpose of a function prototype in C?
ATo declare the function's return type and parameters before its use
BTo define the function's body
CTo allocate memory for the function
DTo call the function
Attempts:
2 left
💡 Hint
Think about what the compiler needs before a function is called.
❓ Predict Output
expert
2:30remaining
Output with mismatched prototype and definition
What is the output of this program?
C
#include <stdio.h>

int foo(int a, int b); // prototype

int main() {
    int result = foo(5, 2);
    printf("%d\n", result);
    return 0;
}

int foo(int a, int b) {
    return a - b;
}
A7
B3
CCompilation error
DUndefined behavior
Attempts:
2 left
💡 Hint
Check if prototype and definition match and what foo returns.

Practice

(1/5)
1. What is the main purpose of a function prototype in C?
easy
A. To allocate memory for the function variables
B. To declare the function's name, return type, and parameters before its definition
C. To execute the function immediately
D. To define the function's full body and logic

Solution

  1. Step 1: Understand what a function prototype declares

    A function prototype tells the compiler the function's name, return type, and parameter types without the body.
  2. Step 2: Differentiate prototype from definition and execution

    It does not define the function's body or execute it; it only declares its interface.
  3. Final Answer:

    To declare the function's name, return type, and parameters before its definition -> Option B
  4. Quick Check:

    Function prototype = declaration before definition [OK]
Hint: Prototypes declare function signature only, no body [OK]
Common Mistakes:
  • Confusing prototype with function definition
  • Thinking prototype runs the function
  • Believing prototype allocates memory
2. Which of the following is a correct function prototype in C for a function named sum that takes two integers and returns an integer?
easy
A. int sum(int a, int b);
B. int sum(int a, int b) {}
C. void sum(int a, int b);
D. int sum(a, b);

Solution

  1. Step 1: Check the syntax of a function prototype

    A prototype includes return type, function name, parameter types, and ends with a semicolon.
  2. Step 2: Evaluate each option

    int sum(int a, int b); matches the correct syntax. int sum(int a, int b) {} has braces (function body), so it's a definition. void sum(int a, int b); has wrong return type (void). int sum(a, b); lacks parameter types.
  3. Final Answer:

    int sum(int a, int b); -> Option A
  4. Quick Check:

    Prototype = return type + name + parameters + semicolon [OK]
Hint: Prototype ends with semicolon and no braces [OK]
Common Mistakes:
  • Adding braces {} in prototype
  • Omitting parameter types
  • Using wrong return type
3. What will be the output of the following C code?
int add(int x, int y);
int main() {
int result = add(3, 4);
printf("%d", result);
return 0;
}
int add(int a, int b) {
return a + b;
}
medium
A. 34
B. Compilation error
C. 7
D. 0

Solution

  1. Step 1: Understand the prototype and function call

    The prototype declares add before main, so the compiler knows its signature.
  2. Step 2: Trace the function execution

    add(3, 4) returns 3 + 4 = 7, which is printed by printf.
  3. Final Answer:

    7 -> Option C
  4. Quick Check:

    Function call returns sum 3+4=7 [OK]
Hint: Prototype allows calling function before definition [OK]
Common Mistakes:
  • Expecting compilation error without prototype
  • Confusing concatenation with addition
  • Ignoring return value
4. Identify the error in the following code snippet:
int multiply(int a, int b);
int main() {
int x = multiply(2, 3);
printf("%d", x);
return 0;
}
int multiply(int a, b) {
return a * b;
}
medium
A. No error, code runs fine
B. Missing return type in function definition
C. Missing semicolon after prototype
D. Incorrect parameter declaration in function definition

Solution

  1. Step 1: Compare prototype and definition parameters

    The prototype declares int multiply(int a, int b); but the definition has int multiply(int a, b) missing type for b.
  2. Step 2: Understand C parameter declaration rules

    Each parameter must have a type. Missing type for b causes a syntax error.
  3. Final Answer:

    Incorrect parameter declaration in function definition -> Option D
  4. Quick Check:

    All parameters need types in definition [OK]
Hint: All parameters must have types in definition [OK]
Common Mistakes:
  • Omitting type for one parameter
  • Assuming prototype fixes definition errors
  • Missing semicolon after prototype
5. You want to declare a function prototype for processData that takes an array of integers and its size, then returns a float average. Which prototype is correct?
hard
A. float processData(int arr[], int size);
B. float processData(int arr, int size);
C. int processData(int[] arr, int size);
D. float processData(int *arr);

Solution

  1. Step 1: Identify correct parameter types for array and size

    In C, an array parameter can be declared as int arr[] or int *arr, but size must be separate.
  2. Step 2: Check return type and parameters

    Function returns a float average, so return type is float. float processData(int arr[], int size); matches this with int arr[] and int size.
  3. Final Answer:

    float processData(int arr[], int size); -> Option A
  4. Quick Check:

    Array parameter + size + float return = correct prototype [OK]
Hint: Array parameter needs type[] and size parameter [OK]
Common Mistakes:
  • Using wrong return type
  • Omitting size parameter
  • Incorrect array parameter syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard