139,002 views
7 votes
7 votes
define a function setbirth, with int parameters monthval and dayval, that returns a struct of type dateofbirth. the function should assign dateofbirth's data member nummonths with monthval and numdays with dayval.

User USMAN FAZIL
by
2.2k points

1 Answer

20 votes
20 votes

To define a function setbirth, with int parameters monthval and dayval, that returns a struct of type dateofbirth,

BirthMonthDay SetBirth(int monthVal, int dayVal) //defining method SetBirth

{

BirthMonthDay type; //defining structure type variable

type.month = monthVal; //holding value

type.day = dayVal;//holding value

return type; //retrun value

}

In the method above, a structure type method "SetBirth" that accepts two integer parameter, that is "monthVal and dayVal" is defined. This method uses typedef for declaring the structure type method.

Inside the method, a structure type variable that is "type" is declared, which holds the method parameter value and uses the return keyword to return its value.

Here's the complete question:

Define a function SetBirth, with int parameters monthVal and dayVal, that returns a struct of type BirthMonthDay. The function should assign BirthMonthDay's data member month with monthVal and day with dayVal.

#include

typedef struct BirthMonthDay_struct {

int month;

int day;

} BirthMonthDay;

/* Your solution goes here */

int main(void) {

BirthMonthDay studentBirthday;

int month;

int day;

scanf("%d %d", &month, &day);

studentBirthday = SetBirth(month, day);

printf("The student was born on %d/%d.\\", studentBirthday.month, studentBirthday.day);

return 0;

}

User Temporarya
by
2.6k points