Download Examples - Department of Computer and Information Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Monad (functional programming) wikipedia , lookup

Structured programming wikipedia , lookup

Recursion (computer science) wikipedia , lookup

Functional programming wikipedia , lookup

Dirac delta function wikipedia , lookup

Subroutine wikipedia , lookup

APL syntax and symbols wikipedia , lookup

C++ wikipedia , lookup

Function object wikipedia , lookup

C syntax wikipedia , lookup

Parameter (computer programming) wikipedia , lookup

Standard ML wikipedia , lookup

Transcript
Department of Computer and Information Science,
School of Science, IUPUI
CSCI N305
Functions
Examples
Dale Roberts
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 <stdio.h>
void print_message(void)
{
printf(“CSCI 230 is fun\n”);
}
int main(void)
{
print_message();
return 0;
}
Output:
CSCI 230 is fun
Dale Roberts
Repeatedly calling functions
Now that we have functionality isolated in a function, the
function can be reused over and over again.
#include <stdio.h>
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
Dale Roberts
Calling functions from with other control structures
The function can be called from within other
control structures.
#include <stdio.h>
void print_message(void)
{
printf(“CSCI 230 is fun\n”);
}
int main(void)
{
int i;
for (i = 1; i <= 5; i++)
print_message();
return 0;
}
Output:
CSCI 230
CSCI 230
CSCI 230
CSCI 230
CSCI 230
is
is
is
is
is
fun
fun
fun
fun
fun
Dale Roberts
Functions with input parameters
Parameters are by default input parameters and
passed by value.
#include <stdio.h>
Output:
Triangular number 10 is 55
Triangular number 20 is 210
Triangular number 50 is 1275
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;
}
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?
Dale Roberts
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 <stdio.h>
int calculate_triangular_number(int n)
{
int i, triangular_number = 0;
Output:
Triangular number 10 is 55
Triangular number 20 is 210
Triangular number 50 is 1275
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;
}
Dale Roberts
More Examples:
Example 1:
…
float mySquare(float);
main()
{
float y;
printf(“%f\n”, mySquare(5.0));
}
float mySquare(float x)
{
return x*x;
}
Old style
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; 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 Roberts
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]
Dale Roberts
Acknowledgements
Some function examples were obtained from
Kochan, Programming in C.
Dale Roberts