Saturday, 12 October 2013

C++ course(seventh section)

C++ course(seventh section) 

seventh section(Functions)

What is a function ?
Functions are like building blocks
They allow complicated programs to be divided into manageable pieces.
Some of the advantages of functions are: 
A programmer can focus on just that part of the program and construct it, debug it, and perfect it 
Different people can work on different functions simultaneously 
If a function is needed in more than one place in a program, or in different programs, you can write it once and use it many times.

Functions are often referred to as modules.
are like miniature programs.
can be put together to form a larger program.

Standard Functions
In college algebra a function is defined as a rule or correspondence between values, called the function’s arguments, and the unique value of the function associated with the arguments
If f(x) = 2x + 5, then f(1) = 7, f(2) = 9, and f(3) = 11
1, 2, and 3 are arguments
7, 9, and 11 are the corresponding values.

Predefined Functions
Void functions - functions that do not have a data type.
Value-returning functions - functions that have a data type.
To use these functions you need to:
include the correct header file
know the name of the function
know the number of parameters, if any
know the data type of each parameter
know the data type of the value computed by the function, called the type of the function

Standard Functions
Some of the predefined mathematical functions are:
sqrt(x)
pow(x,y)
floor(x)
Predefined functions are organized into separate libraries
I/O functions are contained in the header file iostream
Math functions are contained in the header file cmath

1)The Power Function (pow)
pow(x,y) calculates xy, for example: pow(2,3) = 8.0 
Is of the type double or the function pow returns a value of the type double 
x and y are called the parameters (or arguments) of the function pow 
Function pow has two parameters


2)The sqrt and floor Functions
The square root function sqrt(x) 
calculates the non-negative square root of x for x >= 0.0 
sqrt(2.25) is 1.5 
It is of the type double and has only one parameter 
The floor function floor(x) 
calculates the largest whole number not greater than x 
floor(48.79) is 48.0 
It is of the type double and has only one parameter


User-Defined Functions

Void functions - functions that do not have a data type 
Value-returning functions - functions that have a data type 
To build these functions you need to: 
know the name of the function 
know the number of parameters, if any 
know the data type of each parameter 
know the data type of the value computed by the function, called the type of the function.

Since the value returned by a value-returning function is unique, we:
save the value for further calculation
use the value in some calculation
print the value
A value-returning function is either used in an assignment statement or in an output statement such as cout
There is one more thing that is associated with a function:
the code that is required to accomplish the task.
Formal Parameter - A variable declared in the function heading 
Actual Parameter - A variable or expression listed in a call to a function.


Program example:
#include <iostream>
using namespace std;
int sum (int x, int y);      //function prototype
int square (int x);       //function prototype
int main()
{

 int one, two; //Line 1
cout<<"Line 2: sum of 5 and 10 is "<<sum(5,10)<<endl; //Line 2
cout<<"Line 3: Enter two numbers: "; //Line 3
cin>>one>>two; //Line 4
cout<<endl; //Line 5
cout<<"Line 6: sum of "<<one<<" and "<<two<<" is "<<sum(one,two)<<endl; //Line 6
cout<<"Line 7: square of 2, 3, and 4 is "<<square(2)<<"\t"<<
<<square(3)<<"\t"<<square(4)<<endl; //Line 7
return 0;
}
int sum (int x, int y)
{ return x+y; } // end of sum
int square (int x)
{ return x*x; } // end of sqaure

Function Prototype
Function Prototype - function heading without the body of the function
The syntax is:
functionType functionName(parameter list);
It is not necessary to specify the variable name in the parameter list
The data type of each parameter must be specified

Value-returning Functions
The syntax is:
functionType functionName(formal parameter list)
{
statements
}
functionType - type of the value returned by the function
functionType - also called the data type of the value-returning function

Syntax

The syntax of the formal parameter list is: 
dataType identifier, dataType identifier, ...
The syntax for a function call is: 
functionName(actual parameter list)
The syntax for the actual parameter list is: 
expression or variable,expression or variable, ...


Functions
The formal parameter list can be empty
If the formal parameter list is empty
the parentheses are still needed
the function heading of the value-returning function takes either of the following forms:
functionType functionName()
functionType functionName(void)
in a function call the actual parameter is empty
A call to a value-returning function with an empty formal parameter list is: functionName()

Value-Returning Functions
To call a value-returning function:
Use its name, with the actual parameters (if any) in parentheses
There is a one-to-one correspondence between actual and formal parameters
A value-returning function is called in an expression
The expression may be part of an assignment statement or an output statement
A function call in a program results in the execution of the body of the called function.

The return Statement
Once the function computes the value, the function returns this value via the return statement
The syntax of the return statement is:
return expression or variable;
When a return statement executes in a function, the function immediately terminates and the control goes back to the caller
When a return statement executes in the function main, the program terminates.


Flow of Execution
A function call statement results in the transfer of control to the first statement in the body of the called function 
After the last statement of the called function is executed, the control is passed back to the point immediately following the function call 
A value-returning function returns a value 
After executing the function when the control goes back to the caller, the value that the function returns replaces the function call statement.

Unknown

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment

 
biz.