0
0
Cprogramming~10 mins

Return values in C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return the sum of two integers.

C
int add(int a, int b) {
    return [1];
}
Drag options to blanks, or click blank then click option'
Aa - b
Ba + b
Ca * b
Da / b
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to return a value.
2fill in blank
medium

Complete the code to return the maximum of two integers.

C
int max(int x, int y) {
    if (x > y) {
        return [1];
    } else {
        return y;
    }
}
Drag options to blanks, or click blank then click option'
Ax
B0
Cy
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the wrong variable inside the if block.
Returning a constant instead of a variable.
3fill in blank
hard

Fix the error in the function to correctly return the length of a string.

C
#include <string.h>

int string_length(char *str) {
    int len = strlen(str);
    return [1];
}
Drag options to blanks, or click blank then click option'
A&len
Bstr
C*str
Dlen
Attempts:
3 left
💡 Hint
Common Mistakes
Returning the string pointer instead of the length.
Returning the address of the length variable.
4fill in blank
hard

Fill both blanks to return the product of two numbers and print it.

C
#include <stdio.h>

int multiply(int a, int b) {
    return [1];
}

int main() {
    int result = multiply(3, 4);
    printf("Product: %d\n", [2]);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aa * b
Ba + b
Cresult
D3 * 4
Attempts:
3 left
💡 Hint
Common Mistakes
Returning sum instead of product.
Printing the wrong variable or expression.
5fill in blank
hard

Fill all three blanks to create a function that returns the absolute value of an integer and use it in main.

C
#include <stdio.h>

int absolute([1] num) {
    if (num [2] 0) {
        return -num;
    } else {
        return num;
    }
}

int main() {
    int val = -10;
    int abs_val = absolute([3]);
    printf("Absolute value: %d\n", abs_val);
    return 0;
}
Drag options to blanks, or click blank then click option'
Aint
B<
Cval
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong data type for parameter.
Using wrong comparison operator.
Passing wrong variable to the function.