0
0
Cprogramming~20 mins

Why functions are needed - Challenge Your Understanding

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 code without functions
What is the output of this C code that repeats a task without using functions?
C
#include <stdio.h>

int main() {
    int i;
    for(i = 1; i <= 3; i++) {
        printf("Hello, user!\n");
    }
    for(i = 1; i <= 3; i++) {
        printf("Hello, user!\n");
    }
    return 0;
}
AHello, user!\nHello, user!\nHello, user!\nHello, user!\nHello, user!\nHello, user!\n
BCompilation error
CHello, user!\nHello, user!\nHello, user!\nHello, user!\n
DHello, user!\nHello, user!\nHello, user!\n
Attempts:
2 left
💡 Hint
Look at how many times the print statement runs in total.
Predict Output
intermediate
2:00remaining
Output of code using functions
What is the output of this C code that uses a function to print a message multiple times?
C
#include <stdio.h>

void greet() {
    printf("Hello, user!\n");
}

int main() {
    int i;
    for(i = 1; i <= 3; i++) {
        greet();
    }
    for(i = 1; i <= 3; i++) {
        greet();
    }
    return 0;
}
ACompilation error
BHello, user!\nHello, user!\nHello, user!\n
CHello, user!\nHello, user!\nHello, user!\nHello, user!\n
DHello, user!\nHello, user!\nHello, user!\nHello, user!\nHello, user!\nHello, user!\n
Attempts:
2 left
💡 Hint
The function greet() is called six times in total.
🧠 Conceptual
advanced
1:30remaining
Why use functions instead of repeating code?
Which of the following is the main reason to use functions in C programming?
ATo make the program run faster by avoiding loops
BTo organize code, avoid repetition, and make it easier to maintain
CTo use more memory for storing variables
DTo make the program harder to read and understand
Attempts:
2 left
💡 Hint
Think about how functions help when you want to change something in many places.
🔧 Debug
advanced
1:30remaining
Identify the error in function usage
What error will this C code produce?
C
#include <stdio.h>

void greet() {
    printf("Hello!\n");
}

int main() {
    greet;
    return 0;
}
ANo output, because greet is not called properly
BRuntime error: function greet not found
CCompilation error: expected expression before ';' token
DOutput: Hello!
Attempts:
2 left
💡 Hint
Check how functions are called in C.
Predict Output
expert
2:00remaining
Output of nested function calls
What is the output of this C program with nested function calls?
C
#include <stdio.h>

void printA() {
    printf("A");
}

void printB() {
    printA();
    printf("B");
}

int main() {
    printB();
    printf("C\n");
    return 0;
}
A
ABC
B
ACB
C
BAC
D
CAB
Attempts:
2 left
💡 Hint
Follow the order of function calls and prints step by step.