t06B Functions Examples

67 %
33 %
Information about t06B Functions Examples
Education

Published on June 17, 2007

Author: BAWare

Source: authorstream.com

Slide1:  CSCI 230 Functions Examples Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer IUPUI droberts@cs.iupui.edu Simple function in C:  Simple function in C Writing a function does nothing unless it’s called Calling a function transfers flow of control into the function. When the function returns, flow of control returns back to the caller. #include andlt;stdio.handgt; void print_message(void) { printf('CSCI 230 is fun\n'); } int main(void) { print_message(); return 0; } Output: CSCI 230 is fun Repeatedly calling functions:  Repeatedly calling functions Now that we have functionality isolated in a function, the function can be reused over and over again. #include andlt;stdio.handgt; void print_message(void) { printf('CSCI 230 is fun\n'); } int main(void) { print_message(); print_message(); return 0; } Output: CSCI 230 is fun CSCI 230 is fun Calling functions from with other control structures:  Calling functions from with other control structures The function can be called from within other control structures. #include andlt;stdio.handgt; void print_message(void) { printf('CSCI 230 is fun\n'); } int main(void) { int i; for (i = 1; i andlt;= 5; i++) print_message(); return 0; } Output: CSCI 230 is fun CSCI 230 is fun CSCI 230 is fun CSCI 230 is fun CSCI 230 is fun Functions with input parameters:  Functions with input parameters Parameters are by default input parameters and passed by value. #include andlt;stdio.handgt; void calculate_triangular_number(int n) { int i, triangular_number = 0; for (i = 1; i andlt;= n; i++) triangular_number+=i; printf('Triangular number %d is %d\n', n, triangular_number); } int main(void) { calculate_triangular_number(10); calculate_triangular_number(20); calculate_triangular_number(50); return 0; } Output: Triangular number 10 is 55 Triangular number 20 is 210 Triangular number 50 is 1275 Calculating a triangular number, that is summing integers 1 to n, is very common in Computer Science. It is easily solved using summations. Do you know how to calculate the sum using a formula instead of looping? Functions can return a single result:  Functions can return a single result Functions can return a single result through the return value. Just specify a return type on the function declaration, and be certain the return a value inside the function. Now, you can call the function inside an expression. #include andlt;stdio.handgt; int calculate_triangular_number(int n) { int i, triangular_number = 0; for (i = 1; i andlt;= n; i++) triangular_number+=i; return triangular_number; } int main(void) { printf('Triangular number %d is %d\n', 10, calculate_triangular_number(10)); printf('Triangular number %d is %d\n', 20, calculate_triangular_number(20)); printf('Triangular number %d is %d\n', 50, calculate_triangular_number(50)); return 0; } Output: Triangular number 10 is 55 Triangular number 20 is 210 Triangular number 50 is 1275 More Examples::  More Examples: Example 1: … float mySquare(float); main() { float y; printf('%f\n', mySquare(5.0)); } float mySquare(float x) { return x*x; } float mySquare(x) float x; { return x*x; } Example 2: /* calculate the sum from 1+2+…+n */ int sum(int n) { int i, sum = 0; for (i=1; iandlt;=n; i++) sum += i; return(sum); } main() { int j,n; printf('input value n: '); scanf('%d',andamp;n); for (j=1; jandlt;=n; j++) printf('sum from 1 to %d is %d\n',j,sum(j)); } Functional Decomposition :  Functional Decomposition Top-Down Programming, also called functional decomposition, uses functions to hide details of an algorithm inside the function. In the prior example, main called calculate_triangular_number without regards to how the function is implemented. You can write a call to calculate_triangular_number without concerning yourself at that time with the details of operation of that function. All you need to know is that you can develop the function at a later time. The same programming technique that makes programs easier to write also makes programs easier to read. The reader of main can easily see that is it calculating and displaying three triangular numbers. The reader need to sift through all the details of how a triangular number is actually calculated. [Kochan 137] Acknowledgements:  Acknowledgements Some function examples were obtained from Kochan, Programming in C.

Add a comment

Related presentations

Related pages

MATH - HKUST Class Schedule & Quota

T06B (3056) Tu 05:30PM - 06:20PM: ... Sets and functions, real numbers, ... together with some classical examples.
Read more

UU/IT/CP Lectures & Slides

Attendance at all other lectures as well as at the labs and lessons ... illustrated by as many examples as ... Contact persons/functions Staff ...
Read more

The graph of an even functon is symmeric abou he y-axis ...

The graph of an even functon is symmeric abou he y-axis. Functon is said To be odd if f(-x)=-f(x) from MATH 22 at Mapúa Institute of Technology
Read more

Majorization Inequality If the function f is convex on the ...

Majorization Inequality If the function f is convex on the interval I a b and x from MATH 190 at HKUST
Read more

Super-resolution by means of Beurling minimal ...

These examples are of interest in both super-resolution and ... [CR T06b] for matrices that ... be the space of complex-valued continuous functions on T d ...
Read more

FDA Recalls - mdi Consultants Inc.

FDA Recalls December 2012 - June 2012. Recalls as of June 13,2012; Recalls as of June 6, 2012; Recalls as of May ... Examples include: ...
Read more

Recall Archive 28 - FDA Consultants

Recall # Z-0249-2012;4) SKYLight ... part # T06B The intended use of these devices is to etch tooth dentin/enamel in ... Examples of such ...
Read more

UQM Technologies, Inc. Form 10-K

Examples of existing electric vehicle markets that we believe may present ... These devices perform their functions very inefficiently and represent ...
Read more