C Interview Questions on Functions

We discuss various C Language interview questions on Functions. We discuss about how to pass different types of parameters to a function and understand how modifications in a function affect the values in a calling function.

Interview Questions

  1. What are different parts of a function? Can you explain with an example?
  2. What is the difference between declaration and definition of a function? When is a declaration not needed?
  3. What are arguments and What are parameters?
  4. What is the default return value from a function?
  5. What are the return values from main function?
  6. What is the result of this program?
  7. What is the result of this program?
  8. What is the result of this program?
  9. What is the result of this program?

What are different parts of a function? Can you explain with an example?

Below example shows all the different parts of a function.

#include <stdio.h>

int sum(int i,int j); //Declaration of Function

int main()
{
	printf("sum:%d",sum(5,6));//Invocation of Function sum:11
}

int sum(int i, int j) //Definition of Function
{
	return i + j; //Return Value
}

What is the difference between declaration and definition of a function? When is a declaration not needed?

Declaration specifies the return type, function name and the parameters.

int sum(int i,int j); //Declaration of Function

Definition defines what a function does.

int sum(int i, int j) //Definition of Function
{
	return i + j; //Return Value
}

What are arguments and What are parameters?

Parameters are used in the definition of a function. i and j are parameters in the example below.

int sum(int i, int j) //Definition of Function
{
	return i + j; //Return Value
}

Arguments are used in the invocation of the function. 5 and 6 are arguments to function sum in the below example.

	printf("sum:%d",sum(5,6));//Output   sum:11

What is the default return value from a function?

If a function does not return a value, garbage value is returned.

What are the return values from main function?

From main : Typically, a return value of zero implies normal termination; non-zero values signal unusual or erroneous termination conditions.

What is the result of this program?

#include <stdio.h>

void swap(int i, int j) //Definition of Function
{
	int temp = i;
	i = j;
	j = temp;
}


int main()
{
	int a=5, b=6;
	swap(a,b);
	printf("a:%d,b:%d",a,b);//a:5,b:6
}

Program above does NOT swap variables because in C, everything is pass by value. The value of variable a,b are copied into new variables i,j and the values of i and j are swapped in the function. However, the original values remain unchanged.

What is the result of this program?

#include <stdio.h>

void swap(int *pi, int *pj) //Definition of Function
{
	int temp = *pi;
	*pi = *pj;
	*pj = temp;
}


int main()
{
	int a=5, b=6;
	swap(&a,&b);
	printf("a:%d,b:%d",a,b);//a:6,b:5a
}

In the program above, the values a and b are swapped. The address of the variables a and b are passed to the function. So, the function modifies the original values.

What is the result of this program?

#include <stdio.h>

void modify(int marks[]) //Definition of Function
{
	marks[0] = 100;
}

int main()
{
	int marks[] = {95,96,97};
	modify(marks);
	printf("first:%d",marks[0]);//first:100
}

Value of first (index 0) element is modified to 100. When an array is passed, the address of the first element of array is passed. So, the actual array is modified.

What is the result of this program?

#include <stdio.h>

struct Point{
	int x;
	int y;
};

void modify(struct Point p) //Definition of Function
{
	p.x = 50;
}

int main()
{
	struct Point p1;
	p1.x = 20;
	modify(p1);
	printf("p1.x:%d",p1.x);//p1.x:20
}

The modified value in the function is NOT reflected in main. This is because structures are also passed by value. When modify function is called the structure p1 is copied into a new structure and passed as an argument to modify. The new structure is modified but the original structure p1 remains unchanged.

FREE JAVA INTERVIEW GUIDE

ONE SUBSCRIBER EVERY MONTH WINS 100% DISCOUNT CODE FOR INTERVIEW GUIDE WITH 23 VIDEOS

We Respect Your Privacy