0% found this document useful (0 votes)
52 views

CE150 Introduction To Programming With C: Functions

The document discusses functions in C programming. It defines a function as a series of statements that are grouped together and given a name. Functions allow a program to be divided into smaller, easier to understand pieces of code that can be reused. The document provides an example of a function that calculates the average of two numbers. It demonstrates defining the function prototype, defining the function body, and calling the function from the main program. The general form of a function, parameters, return types, and variable scope are also explained.

Uploaded by

Vlad Simizeanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

CE150 Introduction To Programming With C: Functions

The document discusses functions in C programming. It defines a function as a series of statements that are grouped together and given a name. Functions allow a program to be divided into smaller, easier to understand pieces of code that can be reused. The document provides an example of a function that calculates the average of two numbers. It demonstrates defining the function prototype, defining the function body, and calling the function from the main program. The general form of a function, parameters, return types, and variable scope are also explained.

Uploaded by

Vlad Simizeanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

CE150

Introduction to programming with C

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

• General form of a function


• How to call a function

• Scope of variables: Local, global, and static variables


An example
Computing the average
#include <stdio.h>

float average(float, float);

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;
}

float average(float x, float y) {


return (x+y)/2.0;
}
Computing the average
#include <stdio.h>

float average(float, float);

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;
}

float average(float x, float y) {


return (x+y)/2.0;
}
Computing the average
#include <stdio.h>

float average(float, float);

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>

float average(float, float);

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>

float average(float, float);

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>

float average(float, float);

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)

float average(float x, float y) {


return (x+y)/2.0;
}
Computing the average
#include <stdio.h>

float average(float, float);

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)

float average(float x, float y) {


return (x+y)/2.0;
}
Computing the average
#include <stdio.h> Every function terminates with a return
statement, unless it is of void type; the
float average(float, float);
value returned is the function’s output
int main() {
x float
and y are
x, parameters;
y, avg; when we call
the function, the two
printf("Enter values of its arguments
numbers: ");
are copied to%f",
scanf("%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)

float average(float x, float y) {


return (x+y)/2.0;
}
Computing the average
#include <stdio.h>

float average(float, float);

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;
}

float average(float x, float y) {


return (x+y)/2.0;
}
Computing the average
#include <stdio.h>

float average(float x, float y) {


return (x+y)/2.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;
}

• The return type of a function is the type of data it returns


• If the function is not to return anything, then its type is void
General form of function definition
return-type function-name(parameters)
{
variable declarations;
statements;
}

• The return type of a function is the type of data it returns


• If the function is not to return anything, then its type is void
• Each parameter is preceded by its type
• Different parameters are separated by commas
General form of function definition
return-type function-name(parameters)
{
variable declarations;
statements;
}

• The return type of a function is the type of data it returns


• If the function is not to return anything, then its type is void
• Each parameter is preceded by its type
• Different parameters are separated by commas
• The body of the function can include declarations of local variables
and executable statements
Calling a function
Calling a function
• A function call consists of its name, followed by a list of arguments,
enclosed in parentheses:
average(x, y);

• If the parentheses are missing, the function will not be called

• When a function is called, each argument is evaluated, and its value


is assigned to the corresponding parameter of the function
• Since the parameter contains a copy of the argument’s value, any
changes made to the parameter during the execution of the function
do not affect the argument
Calling a function
• A call of a non-void function produces a value which can be stored
in a variable, and then modified:
avg = average(x, y);

• If the value returned by a function is not assigned to a variable, it


will simply get lost

• It may seem odd to ignore the output of a function, but we do this


often; printf returns the number of characters it prints, but we do
not really use this information

• A call of a void function does not produce any value, so it cannot be


assigned to a variable
Variable scope
Variable scope
• The scope of a variable is the part of the program in which it is
known and can be modified

• 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

You might also like