Tuesday, February 24, 2009

Introduction to Programming :: Using Functions

A function is a group of statements that is executed when it is called from some point of the program. The structure of a function call is as follows.


type name ( parameter1, parameter2, ...) { statements }
  • type :is the data type specifier of the data returned by the function.

  • name :is the identifier by which it will be possible to call the function.

  • parameters :Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas.

  • statements is the function's body. It is a block of statements surrounded by braces { }.

In this small program the function subtraction() is being called by the main(). A serious of couts are being used to show different uses of the function call.




If a function does not return any value then the type would be void.


2 comments:

  1. I like the insertion of actual code into your post.

    ReplyDelete
  2. Nice step by step instructions, easy to understand.

    ReplyDelete