0% found this document useful (0 votes)
18 views25 pages

E102f20 Func 1

Uploaded by

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

E102f20 Func 1

Uploaded by

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

ENGR 102 Introduction to

Electromechanical System
Design
Fall 2020
Lecture : Introduction to Functions

ENGR 102 1
More Operators and Control
Structure
The break statement:

Causes immediate exit from innermost


• while( )
• for( ) loop
• switch ( )

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:

Terminates the body of the loop, but instead


of exiting outright like the break statement,
immediately jump to the top of the loop and
run a test of the expression to determine
whether to run the body of the loop again.

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.

Let’s get into the details…..

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:

type name ( argument1, argument2, ...) statement


where:
· type is the type of data returned by the function.
· name is the name by which it will be possible to call the
function.
· arguments (as many as wanted can be specified). Each
argument consists of a type of data followed by its identifier,
like in a variable declaration (for example, int x) and which acts
within the function like any other variable. They allow passing
parameters to the function when it is called. The different
parameters are separated by commas.
· statement is the function's body. It can be a single instruction
or a block of instructions. In the latter case it must be delimited
by curly brackets {}. ENGR 102 8
Function Task 0
// Program with functions - Prototype
#include <stdio.h>
// Declare function prototype
int main ()
{
// Declare variables
// Perform required tasks
// Call the function or functions

//Copy the function prototype here


{
// Function Tasks
// Return value to main function if necessary
}

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.

We can see how the main function begins by


declaring the variable z of type int.

Right after that we see a call to addition function.

ENGR 102 11
The addition Function

If we pay attention we will be able to see the


similarity between the structure of the call
to the function and the declaration of the
function itself in the code lines above:

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

But additionally, return was called with the content of


variable r (return (r);), which at that moment was 8, so
this value is said to be returned by the function.

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:

printf(“\nThe result is \n“);


scanf( “%d”,&z);
// produces the result on the screen
ENGR 102 16
Scope of Variables
• Scope of variables [re] You must consider that
the scope of variables declared within a function
or any other block of instructions is only their own
function or their own block of instructions and
cannot be used outside of them.

• For example, in the previous example it had been


impossible to use the variables a, b or r directly in
function main since they were local variables to
function addition.

• Also, it had been impossible to use the variable z


directly within function addition, since this was a
local variable to the function main.
ENGR 102 17
Scope of Variables
• The scope of local variables is limited to the
same nesting level in which they are
declared.
• Nevertheless you can also declare global
variables that are visible from any point of
the code, inside and outside any function.
• In order to declare global variables you
must do it outside any function or block of
instructions, that means, directly in the
body of the program.
ENGR 102 18
Scope of Variables
#include <stdio.h>

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

x=5 and y=3


The result x-y=
2
ENGR 102 20
void…Functions with No Types
• If you remember the syntax of a function
declaration:

type name ( argument1, argument2 ...) statement

• This declaration begins with a type, that is the type


of the data that will be returned by the function
with the return instruction.

• But what if we want to return no value?


ENGR 102 21
The void Function
• Imagine that we want to make a function
just to show a message on the screen. We do
not need it to return any value, moreover,
we do not need it to receive any parameters.

• For these cases, the void type was devised


in the C language. Take a look at the
following:

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

You might also like