Professional Documents
Culture Documents
Functions
Functions
Functions
main function....... DEFINITION OF FUNCTION: A function shall include the following elements..... Function name Function type List of parameters Local variable declarations Function statements A return statement
There are generally two parts in a function..... Function header Function body
Some languages distinguish between functions which return variables and those which don't. C assumes that every function will return a value. If the programmer wants a return value, this is achieved using the return statement. If no return value is required, none should be used when calling the function. Semicolon is not used at the end of function header. Int sum=0; /*This is global variable*/ Main() { Int i; For(i=0;i<=5;i++) { Square(i); /*This calls the square function*/ } Printf(The sum of squares is %d,sum); Square(number) /*this is the square function*/ { Int number,numsq;
Printf(Enter any number); Scanf(%d,&number); Numsq=number*number; /*this calculates square*/ Sum=sum+numsq; /*This calculates the sum of the squares*/ Printf(The square of %d is %d,number,numsq); Return(sum); /*returns the sum to the main function*/ } } In this program, we are finding the square of the given number using functions...... Here we have used the function square() to calculate the square of the number. After that we also calculate the sum of the squares and finally we return the sum to the main() function for display. We can even pass parameters inside a function.......and a program can have any no of functions.....