Thursday, September 29, 2016

Square of given number using function with an argument and a return value.

/* C program to find square of given number using function.
Use of function with an argument and a return value.

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. 


  1. What is function logic represented here

    1. The main objective of this program is to demonstrate use of function with an argument and a return value.

      Execution 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.

  2. in this program it should be int main()