CE150 Introduction To Programming With C: Functions
CE150 Introduction To Programming With C: Functions
Functions
University of Essex
Autumn Term 2020-2021
What is a function?
• A function is a series of statements that have been grouped
together, and have been given a name
• Each function is essentially a small program, with its own
declarations and statements
Advantages of functions:
• A program can be divided into smaller pieces which are easier to
understand and modify
• We can avoid duplicating code which is used more than once
• A function that was originally part of one program can be re-used
in other programs
Overview
• Example defining and using a function
int main() {
float x, y, avg;
printf("Enter two numbers: ");
scanf("%f %f", &x, &y);
avg = average(x, y);
printf("Average: %f \n", avg);
return 0;
}
int main() {
Declaration of the function average.
float x, y, avg;
This statement defines the prototype
printf("Enter two numbers: ");
of the function, which must be given
scanf("%f %f", &x, &y);
before it is used for the first time.
avg = average(x, y);
printf("Average: %f \n", avg);
return 0;
}
int main() {
Declaration of the function average.
float x, y, avg;
This statement defines the prototype
printf("Enter two numbers: ");
of the function, which must be given
scanf("%f %f", &x, &y);
before it is used for the first time.
avg = average(x, y);
printf("Average: %f \n", avg);
return 0; The prototype must include
}
• the type of the function;
• its name;
float average(float x, float y) •{ the types of its parameters.
return (x+y)/2.0;
}
Computing the average
#include <stdio.h>
int main() {
Declaration of the function average.
float x, y, avg;
This statement defines the prototype
printf("Enter two numbers: ");
of the function, which must be given
scanf("%f %f", &x, &y);
before it is used for the first time.
avg = average(x, y);
printf("Average: %f \n", avg);
Thereturn
prototype
0; allows us to The prototype must include
} know how to call the function
• the type of the function;
(assuming we know what the • its name;
function
float is supposedx,
average(float to float
do). y) •{ the types of its parameters.
return (x+y)/2.0;
}
Computing the average
#include <stdio.h>
int main() {
float x, y, avg;
printf("Enter two numbers: ");
scanf("%f %f", &x, &y);
avg = average(x, y);
printf("Average: %f \n", avg);
return 0; Call the function according to its
} prototype; similar to how we have
been calling library functions.
float average(float x, float y) {
x and y are the input arguments
return (x+y)/2.0;
}
Computing the average
#include <stdio.h>
int main() {
float x, y, avg;
printf("Enter two numbers: ");
scanf("%f %f", &x, &y);
avg = average(x, y);
printf("Average: %f \n",Actual
avg);definition of the function,
return 0; where we specify the statements in
} its body (how the function works)
int main() {
x and y are
float x,parameters;
y, avg; when we call
theprintf("Enter
function, the values of its arguments
two numbers: ");
arescanf("%f
copied to %f",
the function’s
&x, &y);parameters
avg = average(x, y);
printf("Average: %f \n",Actual
avg);definition of the function,
return 0; where we specify the statements in
} its body (how the function works)
int main() {
float x, y, avg;
printf("Enter two numbers: ");
scanf("%f %f", &x, &y);
avg = average(x, y);
printf("Average: %f \n", avg);
return 0;
}
int main() {
We can also define the function altogether
float x, y, avg;
before main, without having to declare its
printf("Enter two numbers: ");
prototype separately
scanf("%f %f", &x, &y);
avg = average(x, y);
printf("Average: %f \n", avg);
return 0;
}
Defining a function
General form of function definition
return-type function-name(parameters)
{
variable declarations;
statements;
}
General form of function definition
return-type function-name(parameters)
{
variable declarations;
statements;
}
• A variable can be
q local: known only within the function it has been declared, and
its value is lost when the function terminates
q global: known to all functions of a program; must be declared
before the definition of any function
q static: it is known only within the function it has been declared,
but its value is not lost when the function terminates
• Most of our variables are local and are only used within a function;
local variables in different functions can have the same name!
Local variables
#include <stdio.h>
void example() {
int x = 10;
printf("(example) x: %d\n", x);
}
int main() {
int x = 5;
printf("(main) x: %d\n", x);
example();
printf("(main) x: %d\n", x);
return 0;
}
Local variables
#include <stdio.h>
In both functions, there is a
void example() { local variable named x.
int x = 10;
printf("(example) x: %d\n", x);
}
int main() {
int x = 5;
printf("(main) x: %d\n", x);
example();
printf("(main) x: %d\n", x);
return 0;
}
Local variables
#include <stdio.h>
In both functions, there is a
void example() { local variable named x.
int x = 10;
printf("(example) x: %d\n", x); x is a name for two different
} variables, one with scope in
main and one with scope in
int main() { example
int x = 5;
printf("(main) x: %d\n", x);
example();
printf("(main) x: %d\n", x);
return 0;
}
Local variables
#include <stdio.h>
In both functions, there is a
void example() { local variable named x.
int x = 10;
printf("(example) x: %d\n", x); x is a name for two different
} variables, one with scope in
main and one with scope in
int main() { example
int x = 5;
printf("(main) x: %d\n", x);
example(); Output:
printf("(main) x: %d\n", x); (main) x: 5
(example) x: 10
return 0;
(main) x: 5
}
Global variables
#include <stdio.h>
int x, y = 20;
void example() {
int x = 10;
printf("(example) x: %d, y: %d \n", x, y);
}
int main() {
x = 5;
printf("(main) x: %d, y: %d \n", x, y);
y = 10;
example();
printf("(main) x: %d, y: %d \n", x, y);
return 0;
}
Global variables
#include <stdio.h> There are two global variables
x and y, and one local variable
int x, y = 20; x in example
void example() {
int x = 10;
printf("(example) x: %d, y: %d \n", x, y);
}
int main() {
x = 5;
printf("(main) x: %d, y: %d \n", x, y);
y = 10;
example();
printf("(main) x: %d, y: %d \n", x, y);
return 0;
}
Global variables
#include <stdio.h> example has access to its local
variable x as well as the global
int x, y = 20; variable y
void example()
int x = 10;
printf("(example) x: %d, y: %d \n", x, y);
}
int main() {
x = 5;
printf("(main) x: %d, y: %d \n", x, y);
y = 10;
example();
printf("(main) x: %d, y: %d \n", x, y);
return 0;
}
Global variables
#include <stdio.h> main has access to the two
global variables x and y
int x, y = 20;
void example()
int x = 10;
printf("(example) x: %d, y: %d \n", x, y);
}
int main() {
x = 5;
printf("(main) x: %d, y: %d \n", x, y);
y = 10;
example();
printf("(main) x: %d, y: %d \n", x, y);
return 0;
}
Global variables
#include <stdio.h> Output:
(main) x: 5, y: 20
int x, y = 20; (example) x: 10, y: 10
(main) x: 5, y: 10
void example()
int x = 10;
printf("(example) x: %d, y: %d \n", x, y);
}
int main() {
x = 5;
printf("(main) x: %d, y: %d \n", x, y);
y = 10;
example();
printf("(main) x: %d, y: %d \n", x, y);
return 0;
}
Static variables
#include <stdio.h>
void example() {
static int x = 0;
x = x + 10;
printf("x = %d \t", x);
}
int main() {
int i;
for (i=0; i<10; i++)
example();
return 0;
}
Static variables
#include <stdio.h>
void example() {
static int x = 0;
x = x + 10;
printf("x = %dStatic
\t",local
x); variable x in example;
} its value is retained even after
the termination of the function
int main() {
int i;
for (i=0; i<10; i++)
example();
return 0;
}
Static variables
#include <stdio.h>
void example() {
static int x = 0;
x = x + 10;
printf("x = %d \t", x);
}
The example function is called 10 times:
int main() { x = 10 x = 20 … x = 100
int i;
for (i=0; i<10; i++)
example();
return 0;
}
Recursion
Recursive functions
• A function is recursive if it calls itself
• For example, suppose that we want to compute the factorial of a
number: n! = n × (n-1)!
• So, to compute n! we can define a function which when given as
input the integer n, it will multiply n with the value returned when
the function is called with argument n-1
• The recursion needs to stop at some point; in particular, when n is
at most 1, we know that the factorial is simply 1
int factorial(int n) {
if (n<=1)
return 1;
else
return n*factorial(n-1);
}
Recursive functions
• To understand how recursion works, let’s trace the execution of the
statement:
k = factorial(4)
= 4*factorial(3)
= 4*(3*factorial(2))
= 4*(3*(2*factorial(1)))
= 4*(3*(2*1))
= 4*(3*2)
= 4*6
= 24