Bird
Raised Fist0
Cprogramming~20 mins

Function declaration and definition - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the purpose of a function declaration in C?
easy
A. To allocate memory for variables inside the function
B. To execute the function's code
C. To tell the compiler about the function's name, return type, and parameters before its use
D. To print the function's output

Solution

  1. Step 1: Understand function declaration role

    A function declaration informs the compiler about the function's name, return type, and parameters so it can be used before its definition.
  2. Step 2: Differentiate declaration from definition

    Declaration does not contain the function's code; that is done in the definition.
  3. Final Answer:

    To tell the compiler about the function's name, return type, and parameters before its use -> Option C
  4. Quick Check:

    Function declaration = Inform compiler [OK]
Hint: Declaration tells compiler about function before use [OK]
Common Mistakes:
  • Confusing declaration with definition
  • Thinking declaration runs the function
  • Assuming declaration allocates memory
2. Which of the following is a correct function declaration in C?
easy
A. int add { int a, int b; };
B. int add(int a, int b) {}
C. add(int a, int b) int;
D. int add(int a, int b);

Solution

  1. Step 1: Identify correct syntax for declaration

    A function declaration includes the return type, function name, parameter list, and ends with a semicolon.
  2. Step 2: Check each option

    int add(int a, int b); matches correct syntax: return type 'int', name 'add', parameters '(int a, int b)', and semicolon. Others have syntax errors or missing semicolon.
  3. Final Answer:

    int add(int a, int b); -> Option D
  4. Quick Check:

    Declaration ends with semicolon [OK]
Hint: Declaration ends with semicolon and no body [OK]
Common Mistakes:
  • Missing semicolon at end
  • Including function body in declaration
  • Wrong parameter syntax
3. What will be the output of this C program?
#include <stdio.h>

int multiply(int x, int y);

int main() {
    printf("%d", multiply(3, 4));
    return 0;
}

int multiply(int x, int y) {
    return x * y;
}
medium
A. 12
B. 7
C. 34
D. Compilation error

Solution

  1. Step 1: Understand function declaration and call

    The function 'multiply' is declared before main and defined after. main calls multiply(3, 4).
  2. Step 2: Calculate function return value

    multiply returns 3 * 4 = 12, which is printed by printf.
  3. Final Answer:

    12 -> Option A
  4. Quick Check:

    3 * 4 = 12 [OK]
Hint: Declaration allows call before definition [OK]
Common Mistakes:
  • Thinking declaration runs code
  • Confusing output with parameters
  • Expecting compilation error due to declaration
4. Identify the error in this function declaration and definition:
int sum(int a, int b)
{
    return a + b
}
medium
A. Function name is invalid
B. Missing semicolon after return statement
C. Parameters must be declared outside parentheses
D. Return type should be void

Solution

  1. Step 1: Check syntax inside function body

    The return statement must end with a semicolon in C.
  2. Step 2: Identify missing semicolon

    Here, 'return a + b' lacks a semicolon, causing a syntax error.
  3. Final Answer:

    Missing semicolon after return statement -> Option B
  4. Quick Check:

    Statements end with semicolon [OK]
Hint: Return statements need semicolon [OK]
Common Mistakes:
  • Omitting semicolon after return
  • Misunderstanding parameter syntax
  • Changing return type unnecessarily
5. You want to declare and define a function that calculates the average of three integers and returns a float. Which of the following is the correct declaration and definition?
hard
A. float average(int a, int b, int c); float average(int a, int b, int c) { return (a + b + c) / 3.0; }
B. int average(int a, int b, int c); int average(int a, int b, int c) { return (a + b + c) / 3.0; }
C. float average(int a, int b, int c); float average(int a, int b, int c) { return (a + b + c) / 3; }
D. float average(int a, int b, int c) { return (a + b + c) / 3; }

Solution

  1. Step 1: Check function declaration matches definition

    Declaration and definition must have same return type (float) and parameters (three ints).
  2. Step 2: Ensure correct calculation for float average

    To get float result, divide by 3.0 (float), not 3 (int).
  3. Step 3: Verify other options

    Integer division (/ 3) causes truncation. Declaring int return but dividing by 3.0 causes type mismatch. Missing declaration prevents prior use.
  4. Final Answer:

    float average(int a, int b, int c); float average(int a, int b, int c) { return (a + b + c) / 3.0; } -> Option A
  5. Quick Check:

    Float division needs 3.0 [OK]
Hint: Use float division (3.0) and matching declaration [OK]
Common Mistakes:
  • Using integer division instead of float
  • Mismatching declaration and definition types
  • Omitting function declaration
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