Functions Questions with Answers

14 practice questions on Functions (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 14 free in the AptiNest app.

Sample questions

Question 1Medium

Functions can return enumeration constants in c?

  1. True
  2. False
  3. Depends on the compiler
  4. Depends on the standard

Answer: A. True

Explanation

If we have something like the following in a header file:

enum Foo
{
   BAR,
   BAZ
};
how do we declare a function that returns an enum of type foo? Can I just do something like the following:

Foo testFunc()
{
    return Foo.BAR;
}

Step-by-step solution

  1. In C, an enumeration constant is an integer constant.
  2. A function can return any integer type, so it can return an enum constant.
  3. This is allowed by the standard in all compilers, so the answer is True.

Why the other options are wrong

  • B. Enum values are integers, and functions can return integers.
  • C. The rule is set by the standard, so it does not depend on the compiler.
  • D. The standard allows it, so the answer does not depend on the standard.

Tip: Enum constants are just int values in C.

Question 2Medium

What is the output of this C code?

 #include <stdio.h>
 void m()
 {
   printf("hi");
 }
 void main()
 {
   m();
 }
  1. hi
  2. Run time error
  3. Nothing
  4. Varies

Answer: A. hi

Explanation

Function Defined in the Global.So No need to declare in main().We will call directly and m() will print Output "hi".

Step-by-step solution

  1. The function m() is defined before main().
  2. m() prints 'hi' when called.
  3. main() calls m() at the start.

Why the other options are wrong

  • B. No runtime errors occur.
  • C. The program does not terminate without output.
  • D. Output is consistent and predictable.

Tip: Always ensure that all function calls are valid and properly placed.

Question 3Medium

There is a error in the below program. Which statement will you add to remove it?

#include <stdio.h>
int main()
{
  int a;
  a = f(10, 3.14);
  printf("%d\n", a);
  return 0;
}
float f(int aa, float bb)
{
  return ((float)aa + bb);
}
  1. Add prototype: float f(float, float)
  2. Add prototype: float f(int, float)
  3. Add prototype: float f(float, int)
  4. Add prototype: float f(bb, aa)

Answer: B. Add prototype: float f(int, float)

Explanation

he correct form of function f prototype is float f(int, float);

Step-by-step solution

  1. The function call uses int and float.
  2. The function definition expects int and float.
  3. No prototype declared before main.

Why the other options are wrong

  • A. It's a question, not a statement.
  • C. Incorrect parameter order in the prototype.

Tip: Always declare function prototypes before main.

Question 4Medium

The value obtained in the function is given back to main by using __ keyword?

  1. return
  2. static
  3. new
  4. volatile

Answer: A. return

Explanation

The return keyword sends a value from a function back to the code that called it, which is main here.

Step-by-step solution

  1. A function sends its value back to the caller using the return keyword.
  2. Static, new and volatile do not send a value back to main.
  3. So the answer is return.

Why the other options are wrong

  • B. Static gives a variable or function internal linkage or lifetime, not a return.
  • C. New is a C++ keyword for allocation, not a way to return a value.
  • D. Volatile tells the compiler a value can change unexpectedly, not return it.

Tip: The return keyword sends a value back to the caller.

Question 5Medium

The keyword used to transfer control from a function back to the calling function is?

  1. switch
  2. goto
  3. go back
  4. return

Answer: D. return

Explanation

The keyword return is used to transfer control from a function back to the calling function.
Example:

#include <stdio.h>
int add(int, int);         /* Function prototype */
int main()
{
 int a = 4, b = 3, c;
 c = add(a, b);
 printf("c = %d", c);
 return 0;
}
int add(int a, int b)
 {
   /* returns the value and control back to main() function */
  return (a+b);
 }

Output:
c = 7

Step-by-step solution

  1. Return ends the current function and sends control back to the function that called it.
  2. Switch chooses one of several branches, and goto jumps to a label in the same function.
  3. Go back is not a C keyword.
  4. So the answer is return.

Why the other options are wrong

  • A. Switch selects a branch; it does not leave a function.
  • B. Goto jumps to a label in the same function; it does not return to the caller.
  • C. Go back is not a keyword in C.

Tip: Return is the keyword for leaving a function, with or without a value.

Question 6Medium

Functions can return structure in c?

  1. True
  2. False
  3. Depends on the compiler
  4. Depends on the standard

Answer: A. True

Explanation

Can return a structure from a function (or use the = operator) without any problems.
It's a well-defined part of the language.
The only problem with struct b = a is that you didn't provide a complete type.
"structMyObj b = a" will work just fine.

You can pass structures to functions as well - a structure is exactly the same as any built-in type for purposes of parameter passing,return values, and assignment.

Here's a simple demonstration program that does all three - passes a structure as a parameter,returns a structure from a function, and uses structures in assignment statements:

#include <stdio.h>
struct a {
int i;
};

struct a f(struct a x)
{
struct a r = x;
return r;
}

int main(void)
{
struct a x = { 12 };
struct a y = f(x);
printf("%d\n", y.i);
return 0;
}

The next example is pretty much exactly the same, but uses the built-in int type for demonstration purposes.
The two programs have the same behaviour with respect to pass-by-value for parameter passing, assignment, etc.:

#include <stdio.h>
int f(int x)
{
int r = x;
return r;
}

int main(void)
{
int x = 12;
int y = f(x);
printf("%d\n", y);
return 0;
}

Step-by-step solution

  1. C allows a function to return a structure by value.
  2. This is part of the C standard, not a compiler extension.
  3. So the answer is True, which is option A.

Why the other options are wrong

  • B. C allows returning structures, so False is wrong.
  • C. The standard allows it, so the answer does not depend on the compiler.
  • D. The standard allows it, so the answer does not depend on the standard.

Tip: Structures can be returned by value in C.

Question 7Medium

Which function definition will run correctly?

  1. int sum(int a, int b return (a + b);
  2. int sum(int a, int b) {return (a + b);}
  3. int sum(a, b) return (a + b);
  4. Both (a) and (b)

Answer: B.

Explanation

The Function defination contain the Body of a function.

int sum(int a, int b)
{
   return(a+b);
}

The Body of function is start and End by '{' and '}' operator.
These operator shows scope of the function.

Step-by-step solution

  1. A function definition needs a parameter list in parentheses, with a type for each parameter, and a body in braces.
  2. Option A is missing the closing parenthesis after int b, so it will not compile.
  3. Option C has no parameter types and no braces, so it is not a valid definition.
  4. Option B is ```c int sum(int a, int b) {return (a + b);} ``` , which is valid, so the answer is option B.

Why the other options are wrong

  • A. The closing parenthesis after int b is missing, so it will not compile.
  • C. Parameter types are missing and the body has no braces, so it is invalid.
  • D. Option A is invalid, so both (a) and (b) cannot be right.

Tip: Check brackets and types in the function header first.

Question 8Medium

Which of the following statements are correct about the program?

#include <stdio.h>
int main()
{
    printf("%p\n", main());
    return 0;
}
  1. It prints garbage values infinitely
  2. Runs infinitely without printing anything
  3. Error: main() cannot be called inside printf()
  4. No Error and print nothing

Answer: B. Runs infinitely without printing anything

Explanation

In printf("%p\n", main()); it calls the main() function and then it repeats infinetly, untill stack overflow.

Step-by-step solution

  1. main() calls itself as an argument to printf, before printf can print anything.
  2. Each call starts main again, which calls main again, so there is no return.
  3. The recursion never ends, and the stack eventually overflows and crashes.
  4. So the program runs infinitely without printing anything.

Why the other options are wrong

  • A. Printf never runs, so no garbage value is printed.
  • C. Main can be called recursively in C, so there is no error.
  • D. The program does not finish; it crashes from endless recursion.

Tip: A function that calls itself with no stopping condition never returns.

Question 9Medium

What is the output of code given below?

#include <stdio.h>
 enum m{JAN, FEB, MAR};
 enum m foo();
 int main()
 {
   enum m i = foo();
   printf("%d\n", i);
 }
 int  foo()
 {
   return JAN;
 }
  1. Compile time error
  2. 0
  3. Depends on the compiler
  4. Depends on the standard

Answer: A. Compile time error

Explanation

If we have something like the following in a header file:

enum Foo
{
   BAR,
   BAZ
};
how do we declare a function that returns an enum of type foo?
Can I just do something like the following:

Foo testFunc()
{
    return Foo.BAR;
}

Step-by-step solution

  1. The prototype enum m foo() declares foo to return enum m.
  2. The definition int foo() gives foo a return type of int.
  3. C requires every declaration of a function to have compatible types; a typical compiler gives enum m the type unsigned int, which is not compatible with int.
  4. The conflicting declaration stops compilation, so the answer is: Compile time error.

Why the other options are wrong

  • B. The program never runs because the conflicting declaration is rejected at compile time.
  • C. The result does not depend on the compiler; any C compiler rejects this code.
  • D. The standard does not allow this conflict, so the answer is not dependent on the standard.

Tip: A function's prototype and definition must have compatible return types.

Question 10Medium

What is the output of this C code?

 #include <stdio.h>
 void foo();
 int main()
 {
   void foo(int);
   foo();
   return 0;
 }
 void foo()
 {
   printf("2 ");
 }
  1. 2
  2. Compile time error
  3. Depends on the compiler
  4. Depends on the standard

Answer: B. Compile time error

Explanation

The foo Function declaration in main is

 void foo(int);
//But the function definition is
void foo()
 {
   printf("2 ");
 }

So both function prototype is mismatched.
Compiler may be execute as a error.

Step-by-step solution

  1. - Inside main, the declaration void foo(int); says foo takes one int argument.
  2. - The call foo() passes no argument, which does not match the declared int parameter.
  3. - A call with the wrong number of arguments breaks a rule of C, so the compiler rejects the program.
  4. - So the answer is Compile time error.

Why the other options are wrong

  • A. The call does not match the prototype, so the program does not run to print 2.
  • C. The result does not depend on the compiler, because the error is a rule.
  • D. The rule is fixed by the standard, so the result does not vary.

Tip: A block-scope prototype still has to match the call.

Practise all 14 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics