/* C program to
find square of given number using function.
Use of
function with an argument and a return value.
*/
#include<stdio.h>
int
square(int); // function prototype declaration.
void main()
{
int number, answer;
printf("Enter your number:");
scanf("%d", &number);
answer = square(number); //Call function.
printf("Square of %d is %d.",
number, answer);
}
int square(int
n)
{
//function logic is written here..
return(n*n); //This will return answer to
main function.
}
|
Output of the
Program:
Enter your number:5
Square of 5 is 25.
This has helped me
ReplyDeleteThanks Veronicah for your appreciation.
DeleteThanks
ReplyDeleteNice
ReplyDeletecool
ReplyDeleteWhat is function logic represented here
ReplyDeleteThe main objective of this program is to demonstrate use of function with an argument and a return value.
DeleteExecution of following statement from main program
answer = square(number); //Call function.
will call the function: int square(int n).
The function will return multiplication of parameter (n) passed during function call. Hence, if function call is square(5); it will return 5 X 5, i.e. 25 value to main program; which is printed using printf() function of main program.
in this program it should be int main()
ReplyDeleteNice programming....try to write the same program for c++
ReplyDeleteawesome one
ReplyDeleteIn this program may have some errors please cjeck it
ReplyDeleteDear Visitor,
DeleteThis program works fine. Share the error which you are facing in this program for further investigation.
Can you write this same program without argument and with return type
ReplyDeleteThis comment has been removed by the author.
DeleteDear Reader, refer the following link to see the C program without argument and a return value: Square of number without argument and a return value.
Deletewhy it said "error :main must return 'int'
ReplyDeleteDear Visitor,
DeleteInstead of void main() you can also use int main() with return 0 at the end of main() function. Try the following code:
=========
#include
int square(int); // function prototype declaration.
int main()
{
int number, answer;
printf("Enter your number:");
scanf("%d", &number);
answer = square(number); //Call function.
printf("Square of %d is %d.", number, answer);
return 0;
}
int square(int n)
{
//function logic is written here..
return(n*n); //This will return answer to main function.
}
Thanks
ReplyDelete