t06B Functions Examples

Uploaded from authorPOINT
Views:
 
Category: Education
     
 

Presentation Description

No description available.

Comments

By: raviprakash7521 (15 month(s) ago)

hello sir ,i am a student of b.tech .so,piease allow me to download this pptt

Presentation Transcript

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.