Classof1 logo
Fax: 1- 425- 458- 9358 | Toll free: 1- 877- 252 - 7763
Bookmark and Share
Forgot Password? Click Here
Register  |  Account

Need help with Computer Science assignment?

Get customized homework help now!

Functions

Function is defined as a named group of statements. A function can be called from any point in a program. If a problem can be divided into small modules then each module can be represented as a function.

Two types of functions in c++. They are

  • Library functions
  • User defined functions.

Library functions: They are predefined functions. E.g.  cin, cout.

User defined functions:

A user defined function has to be written by the programmer to carry out some well defined and specific task.

Function definition:

A function should be defined before it is used. A function has two parts

  • Function header
  • Statement body.

Syntax:

function-type  function-name (list of parameters with declaration)

{

local variable declaration;

executable statement-1;
-- -- - - -- - -- - - -- - -
-- -- - - -- - -- - - -- - -
return(expression);
}

function-type   –    represents the data type of the value returned by the function.

Function- name  –     valid c++ variable name

List of arguments –   formal arguments.

Local variable declaration –  declaration of variables in the statement body.

return  -   to return the value of the function

Rules:

  • Function header should not terminate with semicolon.
  • List of arguments and argument declaration are optional.
  • If the function has no list of arguments an empty parenthesis is a must.

The expression in the return statement is optional .If there is no expression the return statement acts as a closing brace and return back the control. If there is expression it returns the value of the expression.

To declare function-type,The function type in the function header is optional. If there is no function type in the function header the return statement by default returns a integer value.

But with the help of the option function-type in the function header we can declare the type of the value returned by the function.

Eg:Function-name(list of arguments)

his function returns integer value.

Calling a function:

A defined function can be called from other functions by specifying its name followed by a list of arguments enclosed within parenthesis.

Syntax:

Function-name(list of arguments);

Function-name – name of a already defined function.

List of arguments – actual arguments.

Computer Science Homework Help
Name* :
Email* :
Country* :
Phone* :
Subject* :
Upload Homework :
Upload another homework (upto 5 uploads max.)
Due Date
Time
AM/PM
Timezone
Instructions
(Type Security Code - case sensitive)