Example Write a program to display message using user defined functions

Example write a program to display message using user

This preview shows page 102 - 106 out of 125 pages.

Example: Write a program to display message using user-defined functions. #include<stdio.h> #include<conio.h> void main() { void message(); message(); } void message() { puts(“Have a nice day”); } Output: Have a nice day 2 with arguments but without return values
Image of page 102
Arrays and Structures 4.21 4.6.2 With arguments and without return value: Calling function Analysis Called function main() { ……….. ……….. abc(x); ………. ………. } Arguments are passed No values are send back abc(y) { ………… ………… ………… } (1) In the above functions, arguments are passed through the calling function. The called function operates on the values. But no result is sent back (2) 2. such functions are partly dependent on the calling function the result obtained is uti- lised by the called function and there is no gain to the main() Example: Write a program to send value to user-defined function and display result. #include<stdio.h> #include<conio.h> void main() { int dat(int, int, int) int d,m,y; return(); printf(“Enter Date dd/mm/yyyy”); scanf(“%d%d%d”,&d,&m,&y); dat(d,m,y); return 0; } dat(int x, int y, int z) { printf(“Date=%d%d%d”,x,y,z); } Output: Enter Date dd/mm/yyyy 12 12 2001 Date : 12/12/2001.
Image of page 103
4.22 Fundamentals of Computing an Programming 4.6.3 With arguments and return value: Calling function Analysis Called function main() { int z; ……….. z=abc(x); ………. ………. } Arguments are passed Values are send back abc(y) { ………… y++; ………… ………… return(y); } (1) In the above example the copy of actual argument is passed to the formal argument, that is value of is assigned to y (2) The return statement returns the increased value of y. the returned value is collected by z (3) Here data is transferred between calling and the called functions, that is, communications, between functions is made Example: Write a program to pass value to user-defined function collect and display the values returned by the called function. #include<stdio.h> #include<conio.h> void main() { int dat(int, int, int) int d,m,y,t; return(); printf(“Enter Date dd/mm/yyyy”); scanf(“%d%d%d%t”,dat(d,m,y)); tdat(d,m,y); printf(“\n Tomorrow=%d%d%d”,t,m,y); return 0; } dat(int x, int y, int z) { printf(“Today=%d%d%d”,x,y,z);
Image of page 104
Arrays and Structures 4.23 return(++x); } Output: Enter Date dd/mm/yyyy 12 12 2001 Today : 12/12/2001 Tomorrow : 13/12/2001 4.6.4 Without arguments and with return values Calling function Analysis Called function main() { int z; ……….. z=abc(); ………. ………. } No arguments are passed values are send back abc() { int y=5; ………… ………… ………… return(y); } (1) In the above type of function no argument(s) are passed through the main() function. But the called function returns the values (2) The called function is independent. It reads values from the keyboard or generated from initialization and returns the values. (3) Here both the calling and the called functions are partly communicated with each other.
Image of page 105
Image of page 106

You've reached the end of your free preview.

Want to read all 125 pages?

  • Fall '19

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture