Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Functions in C Programming

October 11, 2013 Posted by: GPA Views: 647 0 comments

Most popular computer programs are much larger than the programs presented in our articles. The best way to develop a large program is to construct it from smaller pieces or modules, each of which is the part of program. These smaller pieces and modules in C are called functions. In this lesson we describe simple function that will calculate the sum of two numbers. Name it sum.

Function sum is called in main within the printf statement. This function "sum" receives a copy of the value of firstnumber in the parameter x, and copy of the value of secondnumber in the parameter y. Then function sum calculates x+y. The result is passed back to function printf in main function and printf displays the result. The definition of function "sum" shows that "sum" expects an double parameters x and y. The keyword double before the function "sum" show that function "sum" returns double result.

Remember:  if you write your function after the main function then you must write function prototype before main function.

Function prototype - is one of the most important features of C. A function prototype tells the compiler the type of data returned by the function, the number of parameters that the function receives, the types of the parameters. The compiler uses function prototypes to validate function calls.

Example

  


#include <stdio.h>
#include <stdlib.h>
double sum(double x,double y);
int main(int argc, char *argv[]){
   double firstnumber=35.66;
   double secondnumber=15.14;
   printf("%.2f+%.2f = %.2f\n",firstnumber,secondnumber,sum(firstnumber,secondnumber));
   system("PAUSE");
   return 0;
}
double sum(double x,double y){
  return x+y;
}

Output

Functions in C

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe