Functions play a crucial role in structured programming. In C, they provide modularity, allowing a programmer to break down large programs into small, manageable functions or procedures. If you’re new to C programming or looking to brush up on your knowledge, here’s a quick quiz on functions!
Each question is followed by the correct answer and an explanation to help reinforce your knowledge.
1. What is the main purpose of a function in C?
Answer:
Explanation:
Functions provide modularity, reduce code repetition, and make the code more readable and maintainable.
2. Which of the following is the correct way to declare a function in C?
Answer:
Explanation:
In C, functions are declared with their return type followed by the function name and parentheses.
3. What is the output of the following code?
void greet() {
printf("Hello");
}
int main() {
greet();
return 0;
}
Answer:
Explanation:
The function greet() is called within main() and prints “Hello” to the console.
4. How many values can a function return in C?
Answer:
Explanation:
A function in C can return only one value, or it can be a void function returning no value.
5. What does the ‘void’ keyword represent in a function?
Answer:
Explanation:
The ‘void’ keyword indicates that the function doesn’t return any value.
6. How do you call a function named ‘display’ in C?
Answer:
Explanation:
Functions are called by their name followed by parentheses.
7. If a function doesn’t explicitly return a value, what will its default return type be?
Answer:
Explanation:
If no return type is specified, it defaults to ‘void’ which means the function doesn’t return any value.
8. Which of the following is a valid function declaration?
Answer:
Explanation:
The correct syntax for declaring a function in C is return_type function_name(parameters) { /* code */ }.
9. What is a recursive function?
Answer:
Explanation:
A recursive function is a function that calls itself in its definition.
10. Which of the following is true about function arguments passed by value?
Answer:
Explanation:
When arguments are passed by value, the function works with a copy of the data, not the actual data itself.
11. What is the purpose of the return statement in a function?
Answer:
Explanation:
The return statement is used to return control to the calling function and optionally to return a value.
Congratulations on completing the quiz! Functions are fundamental to efficient coding in C, and understanding them is key to becoming a proficient programmer. Keep practicing and exploring the nuances of functions in C.