E102f20 Func 1
E102f20 Func 1
Electromechanical System
Design
Fall 2020
Lecture : Introduction to Functions
ENGR 102 1
More Operators and Control
Structure
The break statement:
ENGR 102 2
Example of break Statement
for (j=1; j<=100; j++)
{
printf(“\nHey, I’m incrementing!\n”);
if (j==56)
{
break;
}
else
{
printf(“\nKeeps on going…\n”);
}
}
printf(“\nBroke out of loop.\n”);
ENGR 102 3
continue Statement
The continue statement:
ENGR 102 4
Example of continue Statement
for (j=1; j<=100; j++)
{
printf(“\nHey, I’m incrementing!\n”);
if (j==56)
{
continue;
// Goes to top of for loop test if j<=100
}
else
{
printf(“\nKeeps on going…\n”);
}
}
//Out of loop.
ENGR 102 5
Example: Generation of Prime
Numbers
• Use to modulo operator (%) to check
remainder of input to see if it is a prime
number
• That is, perform modulo operation for
integers 2 to (possible_prime - 1) to
determine if result is zero
• If so, then the input, possible_prime is a
prime number
ENGR 102 6
Functions
Using functions, we can structure our
programs in a more modular way, accessing
all the potential that structured
programming in C can offer us.
ENGR 102 7
Function Prototype
A function is a block of instructions that is executed when it is
called from some other point of the program. The following is
its format:
ENGR 102 9
First Function Example
// function example
#include <stdio.h>
int addition (int a, int b)
{
int r;
r =a+b;
return (r);
}
int main ()
{
int z;
z = addition (5,3);
printf(“\nThe result is \n“)
scanf(“%d”, &z);
}
The result is 8
ENGR 102 10
Examining the Function
In order to examine this code, first of all remember
something said at the beginning of this tutorial: a
C program always begins its execution with the
main function. So we will begin there.
ENGR 102 11
The addition Function
ENGR 102 12
“Calling” the addition Function
The parameters have a clear correspondence.
• Within the main function we called to addition
passing two values: 5 and 3 that correspond to the
int a and int b parameters declared for the
function addition.
• At the moment at which the function is called
from main, control is lost by main and passed to
function addition.
• The value of both parameters passed in the call (5
and 3) are copied to the local variables int a and
int b within the function.
ENGR 102 13
Once “Inside” the Function
• Function addition declares a new variable (int r;),
and by means of the expression r=a+b;, it assigns
to r the result of a plus b. Because the passed
parameters for a and b are 5 and 3 respectively, the
result is 8.
• The following line of code:
return (r);
finalizes function addition, and returns the control
back to the function that called it (main) following
the program from the same point at which it was
interrupted by the call to addition.
ENGR 102 14
Value Returned By Function to main
ENGR 102 15
Value Returned By Function
The value returned by a function is the value given
to the function when it is evaluated. Therefore, z
will store the value returned by addition (5, 3),
that is 8. To explain it another way, you can
imagine that the call to a function (addition (5,3))
is literally replaced by the value it returns (8). The
following line of code in main is:
int main()
ENGR 102 19
Another Function Example
#include <stdio.h>
int subtraction (int a, int b)
{
int r;
r=a-b;
return (r);
}
int main ()
{
int x=5, y=3, z;
printf(“\n x = %d and y = %d \n”,x,y);
z = subtraction (x,y);
printf(“\nThe result x - y= \n“);
scanf(“%d”,&z);
}
ENGR 102 22
The void Function Example
// void function example
#include <stdio.h>
void dummyfunction (void)
{
printf(“\nI'm a function!\n“);
}
int main ()
{
dummyfunction ();
Output is:
} I'm a function!
ENGR 102 23
The void Function
• Although in C it is not necessary to specify void, its use is
considered suitable to signify that it is a function without
parameters or arguments and not something else.
• What you must always be aware of is that the format for
calling a function includes specifying its name and
enclosing the arguments between parenthesis. The non-
existence of arguments does not exempt us from the
obligation to use parenthesis.
• For that reason the call to dummyfunction is
dummyfunction ();
This clearly indicates that it is a call to a function and not
the name of a variable or anything else.
ENGR 102 24
The subtraction Function
• In this case we have created the function
subtraction. The only thing that this
function does is to subtract both passed
parameters and to return the result.
• Nevertheless, if we examine the function
main we will see that we have made several
calls to function subtraction. We have used
some different calling methods so that you
see other ways or moments when a function
can be called.
ENGR 102 25