A function prototype is a declaration of a function that specifies its name, return type, and parameters without the function body. It tells the compiler what to expect when the function is called.
Click to reveal answer
beginner
Why do we use function prototypes in C programs?
Function prototypes help the compiler check that functions are called with the correct number and types of arguments, preventing errors before the program runs.
Click to reveal answer
beginner
How do you write a function prototype for a function named add that takes two integers and returns an integer?
The prototype is: int add(int, int);
Click to reveal answer
intermediate
What happens if you call a function without a prototype in C?
If a function is called without a prototype, the compiler assumes it returns an int and does not check the arguments, which can cause unexpected behavior or errors.
Click to reveal answer
beginner
Where should function prototypes be placed in a C program?
Function prototypes are usually placed at the top of the file or in a header file so that all parts of the program know about the functions before they are used.
Click to reveal answer
What does a function prototype in C specify?
AFunction body and variables
BFunction name, return type, and parameters
COnly the function name
DOnly the return type
✗ Incorrect
A function prototype tells the compiler the function's name, return type, and parameter types without the body.
Where is the best place to put function prototypes in a C program?
AAt the top of the file or in a header file
BInside the main function
CAfter the function definitions
DAnywhere in the middle of the code
✗ Incorrect
Placing prototypes at the top or in header files ensures the compiler knows about functions before they are used.
What happens if you call a function without a prototype in C?
AProgram runs perfectly without warnings
BCompiler throws an error immediately
CCompiler assumes it returns int and no argument checks
DFunction is ignored
✗ Incorrect
Without a prototype, the compiler assumes the function returns int and does not check arguments, which can cause bugs.
Which of these is a correct function prototype for a function that returns void and takes no parameters?
Avoid func(void);
Bvoid func();
Cfunc(void);
Dvoid func[];
✗ Incorrect
The correct prototype for a function with no parameters is void func(void); to explicitly specify no arguments.
Why is it important to match the function prototype with the function definition?
AIt is not important
BTo make the program run faster
CTo reduce the size of the executable
DTo avoid compiler errors and ensure correct function calls
✗ Incorrect
Matching prototypes and definitions helps the compiler check calls and prevents errors.
Explain what a function prototype is and why it is useful in C programming.
Think about how the compiler knows what a function looks like before it is used.
You got /3 concepts.
Describe where and how you would declare function prototypes in a C program.
Consider program organization and compiler needs.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a function prototype in C?
easy
A. To allocate memory for the function variables
B. To declare the function's name, return type, and parameters before its definition
C. To execute the function immediately
D. To define the function's full body and logic
Solution
Step 1: Understand what a function prototype declares
A function prototype tells the compiler the function's name, return type, and parameter types without the body.
Step 2: Differentiate prototype from definition and execution
It does not define the function's body or execute it; it only declares its interface.
Final Answer:
To declare the function's name, return type, and parameters before its definition -> Option B
Quick Check:
Function prototype = declaration before definition [OK]
Hint: Prototypes declare function signature only, no body [OK]
Common Mistakes:
Confusing prototype with function definition
Thinking prototype runs the function
Believing prototype allocates memory
2. Which of the following is a correct function prototype in C for a function named sum that takes two integers and returns an integer?
easy
A. int sum(int a, int b);
B. int sum(int a, int b) {}
C. void sum(int a, int b);
D. int sum(a, b);
Solution
Step 1: Check the syntax of a function prototype
A prototype includes return type, function name, parameter types, and ends with a semicolon.
Step 2: Evaluate each option
int sum(int a, int b); matches the correct syntax. int sum(int a, int b) {} has braces (function body), so it's a definition. void sum(int a, int b); has wrong return type (void). int sum(a, b); lacks parameter types.
Final Answer:
int sum(int a, int b); -> Option A
Quick Check:
Prototype = return type + name + parameters + semicolon [OK]
Hint: Prototype ends with semicolon and no braces [OK]
Common Mistakes:
Adding braces {} in prototype
Omitting parameter types
Using wrong return type
3. What will be the output of the following C code?
int add(int x, int y); int main() { int result = add(3, 4); printf("%d", result); return 0; } int add(int a, int b) { return a + b; }
medium
A. 34
B. Compilation error
C. 7
D. 0
Solution
Step 1: Understand the prototype and function call
The prototype declares add before main, so the compiler knows its signature.
Step 2: Trace the function execution
add(3, 4) returns 3 + 4 = 7, which is printed by printf.
Final Answer:
7 -> Option C
Quick Check:
Function call returns sum 3+4=7 [OK]
Hint: Prototype allows calling function before definition [OK]
Common Mistakes:
Expecting compilation error without prototype
Confusing concatenation with addition
Ignoring return value
4. Identify the error in the following code snippet:
int multiply(int a, int b); int main() { int x = multiply(2, 3); printf("%d", x); return 0; } int multiply(int a, b) { return a * b; }
medium
A. No error, code runs fine
B. Missing return type in function definition
C. Missing semicolon after prototype
D. Incorrect parameter declaration in function definition
Solution
Step 1: Compare prototype and definition parameters
The prototype declares int multiply(int a, int b); but the definition has int multiply(int a, b) missing type for b.
Step 2: Understand C parameter declaration rules
Each parameter must have a type. Missing type for b causes a syntax error.
Final Answer:
Incorrect parameter declaration in function definition -> Option D
Quick Check:
All parameters need types in definition [OK]
Hint: All parameters must have types in definition [OK]
Common Mistakes:
Omitting type for one parameter
Assuming prototype fixes definition errors
Missing semicolon after prototype
5. You want to declare a function prototype for processData that takes an array of integers and its size, then returns a float average. Which prototype is correct?
hard
A. float processData(int arr[], int size);
B. float processData(int arr, int size);
C. int processData(int[] arr, int size);
D. float processData(int *arr);
Solution
Step 1: Identify correct parameter types for array and size
In C, an array parameter can be declared as int arr[] or int *arr, but size must be separate.
Step 2: Check return type and parameters
Function returns a float average, so return type is float. float processData(int arr[], int size); matches this with int arr[] and int size.
Final Answer:
float processData(int arr[], int size); -> Option A