Bird
Raised Fist0
Cprogramming~20 mins

Function parameters - Practice Problems & Coding Challenges

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

void update(int *p) {
    *p = *p + 5;
}

int main() {
    int x = 10;
    update(&x);
    printf("%d\n", x);
    return 0;
}
A15
B10
C5
DCompilation error
Attempts:
2 left
💡 Hint
Remember that passing a pointer allows the function to modify the original variable.
❓ Predict Output
intermediate
2:00remaining
Output of function with array parameter
What will this program print?
C
#include <stdio.h>

void modify(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        arr[i] += 1;
    }
}

int main() {
    int nums[3] = {1, 2, 3};
    modify(nums, 3);
    for (int i = 0; i < 3; i++) {
        printf("%d ", nums[i]);
    }
    return 0;
}
A1 2 3
B0 1 2
C2 3 4
DCompilation error
Attempts:
2 left
💡 Hint
Arrays passed to functions can be modified inside the function.
❓ Predict Output
advanced
2:00remaining
Output of function with const pointer parameter
What is the output of this program?
C
#include <stdio.h>

void printValue(const int *p) {
    // *p = 10; // Uncommenting this line causes error
    printf("%d\n", *p);
}

int main() {
    int x = 7;
    printValue(&x);
    return 0;
}
ARuntime error
B10
CCompilation error due to assignment to const pointer
D7
Attempts:
2 left
💡 Hint
The function only reads the value pointed to, it does not modify it.
❓ Predict Output
advanced
2:00remaining
Output of function with multiple parameters and default values (simulate)
What is the output of this program?
C
#include <stdio.h>

void greet(char *name, int times) {
    for (int i = 0; i < times; i++) {
        printf("Hello, %s!\n", name);
    }
}

int main() {
    greet("Alice", 2);
    return 0;
}
A
Hello, Alice!
Hello, Alice!
B
Hello, Alice!
CCompilation error: default parameters not supported
DRuntime error
Attempts:
2 left
💡 Hint
C does not support default parameters, so both arguments must be passed.
🧠 Conceptual
expert
3:00remaining
Behavior of function parameters with struct pointers
Consider this code snippet. What will be the value of 'p.age' after calling 'updatePerson(&p);'?
C
typedef struct {
    int age;
} Person;

void updatePerson(Person *person) {
    person->age = 30;
}

int main() {
    Person p = {25};
    updatePerson(&p);
    // What is p.age here?
    return 0;
}
A25
B30
CUndefined behavior
DCompilation error
Attempts:
2 left
💡 Hint
Passing a pointer to a struct allows modifying its fields inside the function.

Practice

(1/5)
1. What is the purpose of function parameters in C?
easy
A. To send data into functions so they can use it
B. To declare variables inside the main function
C. To print output on the screen
D. To create loops inside functions

Solution

  1. Step 1: Understand function parameters

    Function parameters allow you to pass information into a function so it can use that data.
  2. Step 2: Identify the correct purpose

    Creating loops inside functions, declaring variables inside the main function, and printing output on the screen describe other tasks, not the role of parameters.
  3. Final Answer:

    To send data into functions so they can use it -> Option A
  4. Quick Check:

    Function parameters = send data into functions [OK]
Hint: Parameters pass data into functions for flexibility [OK]
Common Mistakes:
  • Confusing parameters with variables declared inside main
  • Thinking parameters print output
  • Mixing parameters with loop control
2. Which of the following is the correct way to declare a function with two integer parameters in C?
easy
A. void add(int a; int b);
B. void add(int a, b);
C. void add(int a int b);
D. void add(int a, int b);

Solution

  1. Step 1: Check parameter syntax

    Parameters must be separated by commas and each must have a type and name.
  2. Step 2: Identify correct syntax

    void add(int a, int b); correctly uses commas and types. void add(int a; int b); uses semicolon instead of comma. void add(int a int b); misses comma. void add(int a, b); misses type for second parameter.
  3. Final Answer:

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

    Parameters need type and comma [OK]
Hint: Separate parameters with commas and specify type for each [OK]
Common Mistakes:
  • Using semicolons instead of commas between parameters
  • Omitting type for parameters
  • Missing commas between parameters
3. What will be the output of this C program?
void printSum(int x, int y) {
    printf("%d\n", x + y);
}

int main() {
    printSum(3, 4);
    return 0;
}
medium
A. 7
B. 34
C. 0
D. Compilation error

Solution

  1. Step 1: Understand function call and parameters

    The function printSum receives 3 and 4 as parameters and adds them.
  2. Step 2: Calculate the sum and output

    3 + 4 equals 7, so printf prints 7 followed by a newline.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Sum parameters inside function to get output [OK]
Common Mistakes:
  • Thinking parameters concatenate as strings
  • Expecting zero output without return
  • Assuming syntax error due to missing includes
4. Identify the error in this function declaration:
int multiply(int a, b) {
    return a * b;
}
medium
A. Return statement is incorrect
B. Function must return void
C. Missing type for parameter b
D. Function name is invalid

Solution

  1. Step 1: Check parameter declarations

    Each parameter must have a type. Here, 'b' lacks a type.
  2. Step 2: Confirm other parts are correct

    Return type int and function name are valid. Return statement syntax is correct.
  3. Final Answer:

    Missing type for parameter b -> Option C
  4. Quick Check:

    Every parameter needs a type [OK]
Hint: Declare type for every parameter explicitly [OK]
Common Mistakes:
  • Omitting type for second or later parameters
  • Assuming return type must be void
  • Misreading return statement as error
5. You want to write a function that swaps two integers using pointers. Which function parameter list is correct?
hard
A. void swap(int &a, int &b)
B. void swap(int *a, int *b)
C. void swap(int a, int b)
D. void swap(int a*, int b*)

Solution

  1. Step 1: Understand swapping with pointers

    To swap values in C, you pass pointers to the variables so the function can modify original data.
  2. Step 2: Identify correct pointer syntax

    void swap(int *a, int *b) correctly declares two pointer parameters. void swap(int a, int b) passes by value (won't swap original). void swap(int &a, int &b) uses C++ reference syntax (invalid in C). void swap(int a*, int b*) has incorrect pointer syntax.
  3. Final Answer:

    void swap(int *a, int *b) -> Option B
  4. Quick Check:

    Use pointers to modify original variables [OK]
Hint: Use int * for pointer parameters to modify originals [OK]
Common Mistakes:
  • Passing parameters by value when pointers are needed
  • Using C++ reference syntax in C
  • Incorrect pointer declaration 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