lect05_Function

lect05_Function - Functions Functions CSIS1117 Computer...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Functions SIS1117 Computer Programming CSIS1117 Computer Programming 1 c1117 lecture 5
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ontents Contents hat is a nction What is a function ? Predefined function provided by C++ library Mechanism for function calls User defined function function prototype ocal variables Local variables Scope of variables Global variables c1117 lecture 5 2
Background image of page 2
unction Function nction a small program which accomplishes A function is a small program which accomplishes a specific task . e can teract ive input and get output) with We can interact (give input and get output) with functions in our programs for finishing some tasks C++ libraries provide a lot of predefined functions that we can use. For example, sqrt(x) is a function in the cmath library to compute the square root of x ( x is a real number). c1117 lecture 5 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Passing input to e function qrt int main(){ double sqrt(…){ the function sqrt x ... ... qrt(x); ... } sqrt(x); ... } In cmath library Getting output from x Our program the function sqrt We don t need to write codes for finding the square root of a number (actually it is difficult to do so), we can call c1117 lecture 5 4 the function sqrt to help.
Background image of page 4
e can ll r voke a nction sing its ame We can call (or invoke ) a function using its name with necessary input ( arguments ) he type of the arguments are specified for each The type of the arguments are specified for each function t most ne alue returned by a nction s the At most one value is returned by a function as the output ( return value ). he type of the return value is specified for each The type of the return value is specified for each function he nction call qrt(x) an xpression The function call , sqrt(x) is an expression We can place a function call in any places where an xpression expected c1117 lecture 5 5 expression is expected.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
unction Function e usually use the following notation to describe a We usually use the following notation to describe a function: double sqrt(double x) Data type for the return value Data type for the argument sqrt In calling a function , we need to consider: The name of the function double 1 Type of the return value Number of arguments double of each argument / function call c1117 lecture 5 6 // function call double val = sqrt(9.0)
Background image of page 6
he program that voke e function is the ller The program that invoke the function is the caller and the called function is the callee . e need to specify the rary which contains the We need to specify the library , which contains the code of the predefined functions , in our program. g include <cmath> r using qrt() e.g. #include <cmath> for using sqrt() Example: Try to find the area of a triangle given 3 sides a, b and c. The two roots can be calculated by the following formula: s = (a+b+c)/2 area= {s(s-a)(s-b)(s-c) c1117 lecture 5 7
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
xample Example include <iostream> We need to include the library in #include <iostream> #include <cmath> sing namespace std; y using the predefined function gp ; int main() { double a,b,c,s,area; Invoke the qrt nction cin >> a >> b >> c;
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 50

lect05_Function - Functions Functions CSIS1117 Computer...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online