Consider the following ANSI C function:

int SomeFunction (int x, int y)

{

if ( (x == 1) I I (y == 1)) return 1;

if (x == y) return x;

if (x > y) return SomeFunction(x - y, y);

if (y > x) return SomeFunction(x, y - x);

}

The value returned by SomeFunction(15, 255) is _______.

This question was previously asked in
GATE CS 2021 Official Paper: Shift 2
View all GATE CS Papers >

Answer (Detailed Solution Below) 15

Free
GATE CS Full Mock Test
5.5 K Users
65 Questions 100 Marks 180 Mins

Detailed Solution

Download Solution PDF

Answer : 15 to 15 

Explanation :

SomeFunction( x, y )

  • SomeFunction( 15, 255 )
  • SomeFunction( 15 , 255-15)
  • SomeFunction( 15, 240 )

{   y > x (255>15)  }

{ again  y > x (240 > 15) }

  • SomeFunction( 15, 225 )

{ again  y > x }

And so on there will be situation when both x and y become equal.

SomeFunction(15, 15 ) -> {  return x; } so 15 will be returned.

Note :

  • In case , If you wondering why call to SomeFunction(15 ,15) will be made? Why this particular call will be in execution flow?
  • because , we encountered SomeFunction(15, 225)  and 152 == 225 ; and we are subtracting 15 from variable y ( y=225) 
Latest GATE CS Updates

Last updated on Jan 8, 2025

-> GATE CS 2025 Admit Card has been released on 7th January 2025.

-> The exam will be conducted on 1st February 2025 in 2 shifts.

-> Candidates applying for the GATE CE must satisfy the GATE Eligibility Criteria.

-> The candidates should have BTech (Computer Science). Candidates preparing for the exam can refer to the GATE CS Important Questions to improve their preparation.

-> Candidates must check their performance with the help of the GATE CS mock tests and GATE CS previous year papers for the GATE 2025 Exam.

More Function Recursion Questions

Hot Links: teen patti club teen patti master new version teen patti casino download