DOC PREVIEW
IUPUI CSCI 23000 - Functions

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Simple function in CRepeatedly calling functionsCalling functions from with other control structuresFunctions with input parametersFunctions can return a single resultMore Examples:Functional DecompositionAcknowledgementsDale RobertsCSCI 230Functions ExamplesDepartment of Computer and Information Science,School of Science, IUPUIDale Roberts, LecturerDale Roberts, [email protected]@cs.iupui.eduDale RobertsSimple function in CSimple function in CWriting a function does nothing unless it’s calledWriting a function does nothing unless it’s calledCalling a function transfers flow of control into the function.Calling a function transfers flow of control into the function.When the function returns, flow of control returns back to the caller.When the function returns, flow of control returns back to the caller.#include <stdio.h>#include <stdio.h>void print_message(void)void print_message(void){{ printf(“CSCI 230 is fun\n”);printf(“CSCI 230 is fun\n”);}}int main(void)int main(void){{ print_message();print_message(); return 0;return 0;}}Output:Output:CSCI 230 is funCSCI 230 is funDale RobertsRepeatedly calling functionsRepeatedly calling functionsNow that we have functionality isolated in a function, the Now that we have functionality isolated in a function, the function can be reused over and over again.function can be reused over and over again.#include <stdio.h>#include <stdio.h>void print_message(void)void print_message(void){{ printf(“CSCI 230 is fun\n”);printf(“CSCI 230 is fun\n”);}}int main(void)int main(void){{ print_message();print_message(); print_message();print_message(); return 0;return 0;}}Output:Output:CSCI 230 is funCSCI 230 is funCSCI 230 is funCSCI 230 is funDale RobertsCalling functions from with other control structuresCalling functions from with other control structuresThe function can be called from within other The function can be called from within other control structures.control structures.#include <stdio.h>#include <stdio.h>void print_message(void)void print_message(void){{ printf(“CSCI 230 is fun\n”);printf(“CSCI 230 is fun\n”);}}int main(void)int main(void){{ int i;int i; for (i = 1; i <= 5; i++)for (i = 1; i <= 5; i++) print_message();print_message(); return 0;return 0;}}Output:Output:CSCI 230 is funCSCI 230 is funCSCI 230 is funCSCI 230 is funCSCI 230 is funCSCI 230 is funCSCI 230 is funCSCI 230 is funCSCI 230 is funCSCI 230 is funDale RobertsFunctions with input parametersFunctions with input parametersParameters are by default input parameters and Parameters are by default input parameters and passed by value.passed by value.#include <stdio.h>void calculate_triangular_number(int n){ int i, triangular_number = 0; for (i = 1; i <= 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 55Triangular number 20 is 210Triangular number 50 is 1275Calculating 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?Dale RobertsFunctions can return a Functions can return a singlesingle result resultFunctions can return a single result through the return value. Just Functions can return a single result through the return value. Just specify a return type on the function declaration, and be certain the specify a return type on the function declaration, and be certain the return a value inside the function. Now, you can call the function return a value inside the function. Now, you can call the function inside an expression.inside an expression.#include <stdio.h>int calculate_triangular_number(int n){ int i, triangular_number = 0; for (i = 1; i <= 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 55Triangular number 20 is 210Triangular number 50 is 1275Dale RobertsMore Examples:More Examples:Example 1: …float mySquare(float);main() { float y; printf(“%f\n”, mySquare(5.0)); }float mySquare(float x) {return x*x;return x*x;}}float mySquare(x)float x; {return x*x;}Old styleExample 2: /* calculate the sum from 1+2+…+n */ int sum(int n) {int i, sum = 0;for (i=1; i<=n; i++) sum += i;return(sum); } main() {int j,n;printf(“input value n: “);scanf(“%d”,&n);for (j=1; j<=n; j++) printf(“sum from 1 to %d is %d\n”,j,sum(j)); }Dale RobertsFunctional Decomposition Functional Decomposition Top-Down Programming, also called functional Top-Down Programming, also called functional decomposition, uses functions to hide details of an decomposition, uses functions to hide details of an algorithm inside the function.algorithm inside the function.In the prior example, main called In the prior example, main called calculate_triangular_number without regards to how the calculate_triangular_number without regards to how the function is implemented. You can write a call to function is implemented. You can write a call to calculate_triangular_number without concerning calculate_triangular_number without concerning yourself at that time with the details of operation of that yourself at that time with the details of operation of that function. All you need to know is that you function. All you need to know is that you cancan develop develop the function at a later time.the function at a later time.The same programming technique that makes programs The same programming technique that makes programs easier to write also makes programs easier to read. The easier to write also makes programs easier to read. The reader of main can easily see that is it calculating and reader of main can easily see that is it calculating and displaying three triangular numbers. The reader need to displaying three triangular numbers. The reader need to sift through all the details of how a triangular number is sift through all the details of how a triangular number is actually calculated. [Kochan


View Full Document

IUPUI CSCI 23000 - Functions

Download Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?