1 Functions Knowledge Understanding of the structured design using function Skills Writing programs...
-
date post
21-Dec-2015 -
Category
Documents
-
view
219 -
download
0
Transcript of 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs...
![Page 1: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/1.jpg)
1
FunctionsFunctions
KnowledgeUnderstanding of the structured design using function
SkillsWriting programs using user-defined functions and library functions
![Page 2: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/2.jpg)
TK1913-C ProgrammingTK1913-C Programming 22
An approach used to handle complexity in solving problems based on the “divide and conquer” strategy
A larger problem (more complex) is broken up into smaller problems (less complex)
The solution to the problem (a module) consists of a combination of solutions for the smaller problems (sub-modules)
Each sub-module also has its own input(s) and output(s)
Structural DecompositionStructural Decomposition
![Page 3: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/3.jpg)
TK1913-C ProgrammingTK1913-C Programming 33
The results of structural decomposition may be illustrated using a diagram called a structure chart
It is intended to show the structural relationship between sub-modules that construct the problem solution and the data flow between them
Structure ChartsStructure Charts
![Page 4: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/4.jpg)
TK1913-C ProgrammingTK1913-C Programming 44
Read 3 letters Output the first letterFind the first letter
Find the first letterbetween the 2 letters
Find the first letterbetween the 2 letters
letter1,letter2,letter3
firstLetter
firstLetter
firstLetter
letter1,letter2,letter3
firstLetter
letter1,letter2
firstLetter,
letter3
Input 3 letters andoutput the first letter
by chronological order
![Page 5: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/5.jpg)
TK1913-C ProgrammingTK1913-C Programming 55
Top-down design is a method of building
programs based on solution structures derived
from structured decomposition
Solution to every sub-problem in the structure
chart is usually coded as a function in a program
Function is a subprogram which carries out a
specific task
Top-Down DesignTop-Down Design
![Page 6: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/6.jpg)
TK1913-C ProgrammingTK1913-C Programming 66
Functions designed, written or defined by
programmers
Every user-defined function must have a
definition and a declaration (prototype).
A function must be called in order to execute the
statements contained in its definition
Function prototypes are usually placed before
their definitions in a program
User-Defined FunctionsUser-Defined Functions
![Page 7: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/7.jpg)
TK1913-C ProgrammingTK1913-C Programming 77
Program to display 3 lines of characters
Display character line
Example:
![Page 8: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/8.jpg)
TK1913-C ProgrammingTK1913-C Programming 88
Example:#include <stdio.h>void displayLine( );int main() {
int i;for (i=0; i < 3; i++)
displayLine( );}void displayLine( ) {
printf("#######\n");}
main( ) function
displayLine( ) function
![Page 9: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/9.jpg)
TK1913-C ProgrammingTK1913-C Programming 99
Example:#include <stdio.h>void displayLine( );int main() {
int i;for (i=0; i < 3; i++)
displayLine( );}void displayLine( ) {
printf("#######\n");}
displayLine( ) function prototype
displayLine( ) function
![Page 10: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/10.jpg)
TK1913-C ProgrammingTK1913-C Programming 1010
Example:#include <stdio.h>void displayLine( );int main() {
int i;for (i=0; i < 3; i++)
displayLine( );}void displayLine( ) {
printf("#######\n");}
Function head
![Page 11: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/11.jpg)
TK1913-C ProgrammingTK1913-C Programming 1111
Example:#include <stdio.h>void displayLine( );int main() {
int i;for (i=0; i < 3; i++)
displayLine( );}void displayLine( ) {
printf("#######\n");}
Function body
![Page 12: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/12.jpg)
TK1913-C ProgrammingTK1913-C Programming 1212
Example:#include <stdio.h>void displayLine( );int main() {
int i;for (i=0; i < 3; i++)
displayLine( ); }void displayLine( ) {
printf("#######\n");} Function call
- A call to displayLine( ) function
![Page 13: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/13.jpg)
TK1913-C ProgrammingTK1913-C Programming 1313
Example:#include <stdio.h>void displayLine( );int main() {
int i;for (i=0; i < 3; i++)
displayLine( );}void displayLine( ) {
printf("#######\n");}
Another function call- a call to printf( ) library function
![Page 14: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/14.jpg)
TK1913-C ProgrammingTK1913-C Programming 1414
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
0i
![Page 15: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/15.jpg)
TK1913-C ProgrammingTK1913-C Programming 1515
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
0i
![Page 16: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/16.jpg)
TK1913-C ProgrammingTK1913-C Programming 1616
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
0i
![Page 17: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/17.jpg)
TK1913-C ProgrammingTK1913-C Programming 1717
#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
Example:
#######_
![Page 18: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/18.jpg)
TK1913-C ProgrammingTK1913-C Programming 1818
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
0i
#######_
![Page 19: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/19.jpg)
TK1913-C ProgrammingTK1913-C Programming 1919
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
1i
#######_
![Page 20: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/20.jpg)
TK1913-C ProgrammingTK1913-C Programming 2020
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
1i
#######_
![Page 21: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/21.jpg)
TK1913-C ProgrammingTK1913-C Programming 2121
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
1i
#######_
![Page 22: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/22.jpg)
TK1913-C ProgrammingTK1913-C Programming 2222
#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
Example:
############## _
![Page 23: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/23.jpg)
TK1913-C ProgrammingTK1913-C Programming 2323
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
1i
############## _
![Page 24: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/24.jpg)
TK1913-C ProgrammingTK1913-C Programming 2424
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
2i
############## _
![Page 25: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/25.jpg)
TK1913-C ProgrammingTK1913-C Programming 2525
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
2i
############## _
![Page 26: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/26.jpg)
TK1913-C ProgrammingTK1913-C Programming 2626
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
2i
##############_
![Page 27: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/27.jpg)
TK1913-C ProgrammingTK1913-C Programming 2727
#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
Example:
##################### _
![Page 28: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/28.jpg)
TK1913-C ProgrammingTK1913-C Programming 2828
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
2i
##################### _
![Page 29: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/29.jpg)
TK1913-C ProgrammingTK1913-C Programming 2929
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
3i
##################### _
![Page 30: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/30.jpg)
TK1913-C ProgrammingTK1913-C Programming 3030
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
3i
##################### _
![Page 31: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/31.jpg)
TK1913-C ProgrammingTK1913-C Programming 3131
Example:#include <stdio.h>void displayLine( );int main() {
int i;for ( i=0 ; i < 3 ; i++ )
displayLine( );}void displayLine( ) {
printf("#######\n");}
3i
##################### _
![Page 32: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/32.jpg)
TK1913-C ProgrammingTK1913-C Programming 3232
To execute a function, it must first be called with the following information: Stating the function name Providing a certain information (parameter)
Syntax for a function call:function_name( actual_parameter_list );
function_name – the function name that is to be called actual_parameter_list – is a list of information which is
required to execute a function The number of parameters provided must be the same as
defined in the function definition
Function CallsFunction Calls
![Page 33: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/33.jpg)
TK1913-C ProgrammingTK1913-C Programming 3333
A function prototype declares a certain information
pertaining to the function
C compiler uses the information in the function
prototype for compiling function calls in a program
Syntax for function prototyping:
data_type function_name( parameter_definition_list); This form is similar to the function definition head
Function PrototypesFunction Prototypes
![Page 34: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/34.jpg)
TK1913-C ProgrammingTK1913-C Programming 3434
Function characteristics:
With parameters? Returns a value?
Function CharacteristicsFunction Characteristics
![Page 35: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/35.jpg)
TK1913-C ProgrammingTK1913-C Programming 3535
A function which has no parameter and return value may be defined as follows:
void function_name() { local_variable _declaration;
executable_function_statements;}
Function which does not return any value
void
Function without any parameter
( )
Function Without Parameter Function Without Parameter and Not Returning Any Valueand Not Returning Any Value
![Page 36: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/36.jpg)
TK1913-C ProgrammingTK1913-C Programming 3636
Example:#include <stdio.h>void inputComputeAndOutputArea( );int main() {
inputComputeAndOutputArea( );}void inputComputeAndOutputArea( ) {
int width, height, area;scanf("%d%d", &width, &height);area = width * height;printf(“Area: %d\n", area);
}
![Page 37: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/37.jpg)
TK1913-C ProgrammingTK1913-C Programming 3737
Example:#include <stdio.h>void inputComputeAndOutputArea( );int main() {
inputComputeAndOutputArea( );}void inputComputeAndOutputArea( ) {
int width, height, area;scanf("%d%d", &width, &height);area = width * height;printf(“Area: %d\n", area);
}
![Page 38: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/38.jpg)
TK1913-C ProgrammingTK1913-C Programming 3838
#include <stdio.h>void inputComputeAndOutputArea( );int main() {
inputComputeAndOutputArea( );}void inputComputeAndOutputArea( ) {
int width, height, area;scanf("%d%d", &width, &height);area = width * height;printf(“Area: %d\n", area);
}
Example:
_
width
height
area
???
???
???
![Page 39: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/39.jpg)
TK1913-C ProgrammingTK1913-C Programming 3939
#include <stdio.h>void inputComputeAndOutputArea( );int main() {
inputComputeAndOutputArea( );}void inputComputeAndOutputArea( ) {
int width, height, area;scanf("%d%d", &width, &height);area = width * height;printf(“Area: %d\n", area);
}
Example:
12 8_
width
height
area
12
8
???
![Page 40: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/40.jpg)
TK1913-C ProgrammingTK1913-C Programming 4040
#include <stdio.h>void inputComputeAndOutputArea( );int main() {
inputComputeAndOutputArea( );}void inputComputeAndOutputArea( ) {
int width, height, area;scanf("%d%d", &width, &height);area = width * height;printf(“Area: %d\n", area);
}
Example:
12 8_
width
height
area
12
8
96
![Page 41: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/41.jpg)
TK1913-C ProgrammingTK1913-C Programming 4141
#include <stdio.h>void input_compute_and_output_area( );int main() {
inputComputeAndOutputArea( );}void inputComputeAndOutputArea( ) {
int width, height, area;scanf("%d%d", &width, &height);area = width * height;printf(“Area: %d\n", area);
}
Example:
12 8Area: 96_
width
height
area
12
8
96
![Page 42: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/42.jpg)
TK1913-C ProgrammingTK1913-C Programming 4242
#include <stdio.h>void inputComputeAndOutputArea( );int main() {
inputComputeAndOutputArea( );}void inputComputeAndOutputArea( ) {
int width, height, area;scanf("%d%d", &width, &height);area = width * height;printf(“Area: %d\n", area);
}
Example:
12 8Area: 96_
![Page 43: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/43.jpg)
TK1913-C ProgrammingTK1913-C Programming 4343
In structural decomposition, the solution to a sub-problem may require some specific data
Input 3 letters and output the first letter by chronological order
Read 3 letters Output first letterFind first letter
letter 1,letter 2,letter 3 first
letter
letter 1,letter 2,letter 3
firstletter
Function With ParametersFunction With Parameters
![Page 44: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/44.jpg)
TK1913-C ProgrammingTK1913-C Programming 4444
For a C function, a parameter needs to be defined for each data to be handed by a caller
void outputFirstLetter(char firstLetter ) {printf(“First letter: %c\n", firstLetter);
}The caller to this function needs to hand a data of type char. In this function, the data is kept in a variable named firstLetter
Function With ParametersFunction With Parameters
![Page 45: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/45.jpg)
TK1913-C ProgrammingTK1913-C Programming 4545
Parameter’s name is optional in function prototype
void displayArea(int width, int height) {printf(“Area: %d\n", width*height);
}
The following function prototypes are valid:
void displayArea(int width, int height);void displayArea(int, int);
Function With ParametersFunction With Parameters
![Page 46: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/46.jpg)
Example:#include <stdio.h>void computeAndOutputArea(int width, int height);int main() {
int rectWidth, rectHeight;scanf(“%d%d”, &rectWidth, &rectHeight);computeAndOutputArea(rectWidth, rectHeight);
}void computeAndOutputArea(int width, int height) {
printf(“Area: %d\n", width * height);}
![Page 47: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/47.jpg)
Example:#include <stdio.h>void computeAndOutputArea(int width, int height);int main() {
int rectWidth, rectHeight;scanf(“%d%d”, &rectWidth, &rectHeight);computeAndOutputArea(rectWidth, rectHeight);
}void computeAndOutputArea(int width, int height) {
printf(“Area: %d\n", width * height);}
???
???
rectWidth
rectHeight
_
![Page 48: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/48.jpg)
Contoh:
#include <stdio.h>void computeAndOutputArea(int width, int height);int main() {
int rectWidth, rectHeight;scanf(“%d%d”, &rectWidth, &rectHeight);computeAndOutputArea(rectWidth, rectHeight);
}void computeAndOutputArea(int width, int height) {
printf(“Area: %d\n", width * height);}
7
8
rectWidth
rectHeight
7 8_
![Page 49: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/49.jpg)
Example:
#include <stdio.h>void computeAndOutputArea(int width, int height);int main() {
int rectWidth, rectHeight;scanf(“%d%d”, &rectWidth, &rectHeight);computeAndOutputArea(rectWidth, rectHeight);
}void computeAndOutputArea(int width, int height) {
printf(“Area: %d\n", width * height);}
7
8
rectWidth
rectHeight
7 8_
![Page 50: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/50.jpg)
Example:
#include <stdio.h>void computeAndOutputArea(int width, int height);int main() {
int rectWidth, rectHeight;scanf(“%d%d”, &rectWidth, &rectHeight);computeAndOutputArea(rectWidth, rectHeight);
}void computeAndOutputArea(int width, int height) {
printf(“Area: %d\n", width * height);}
7
8
rectWidth
rectHeight
7 8_
7
8
width
height
![Page 51: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/51.jpg)
#include <stdio.h>void computeAndOutputArea(int width, int height);int main() {
int rectWidth, rectHeight;scanf(“%d%d”, &rectWidth, &rectHeight);computeAndOutputArea(rectWidth, rectHeight);
}void computeAndOutputArea( int width, int height) {
printf(“Area: %d\n", width * height);}
Example:
7 8Area: 56_
7
8
width
height
![Page 52: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/52.jpg)
#include <stdio.h>void computeAndOutputArea(int width, int height);int main() {
int rectWidth, rectHeight;scanf(“%d%d”, &rectWidth, &rectHeight);computeAndOutputArea(rectWidth, rectHeight);
}void computeAndOutputArea( int width, int height) {
printf(“Area: %d\n", width * height);}
Example:
7 8Area: 56_
7
8
rectWidth
rectHeight
![Page 53: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/53.jpg)
TK1913-C ProgrammingTK1913-C Programming 5353
In structural decomposition, the solution to a sub-problem may return a data/info
Input 3 letters and Output first letter
Read 3 letters Output first letterFind first letter
letter1,letter2,letter3
firstLetter
letter1,letter2,letter3
firstLetter
Function That Returns a ValueFunction That Returns a Value
![Page 54: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/54.jpg)
TK1913-C ProgrammingTK1913-C Programming 5454
Besides receiving data from the caller, a function can also return a value to the caller.
Example:
int computeRectArea(int width, int height) {int area;area = width * height;return area;
}
Function That Returns a ValueFunction That Returns a Value
![Page 55: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/55.jpg)
TK1913-C ProgrammingTK1913-C Programming 5555
The data type of the value being returned must
be specified in the function definition.return_type function_name(parameter_list) {
local_variable_definition;statements;
}
Value type being returned by the function
return_type
Function That Returns a ValueFunction That Returns a Value
![Page 56: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/56.jpg)
TK1913-C ProgrammingTK1913-C Programming 5656
A function returns a value via a return statement
When a return statement is executed, program control is returned to the caller along with the return value
Example:int computeRectArea(int width, int height) {
int area;area = width * height;return area;
}
return Statementreturn Statement
![Page 57: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/57.jpg)
Example:#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
![Page 58: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/58.jpg)
Example:#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
???
???
rectWidth
rectHeight
???
rectArea
_
![Page 59: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/59.jpg)
Example:#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
7
11
rectWidth
rectHeight
???
rectArea
7 11_
![Page 60: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/60.jpg)
Example:#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
7
11
rectWidth
rectHeight
???
rectArea
7 11_
![Page 61: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/61.jpg)
Example:#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
7
11
rectWidth
rectHeight
???
rectArea
7 11_
7 11
width height
![Page 62: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/62.jpg)
#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
Example:
7 11_
7 11width height
77
![Page 63: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/63.jpg)
#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
Example:
7 11_
7
11
rectWidth
rectHeight
77
rectArea
![Page 64: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/64.jpg)
#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
Example:
7 11_
7
11
rectWidth
rectHeight
77
rectArea
![Page 65: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/65.jpg)
#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
Example:
7 11_
7
11rectWidth
rectHeight
77
rectArea
77
area
![Page 66: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/66.jpg)
#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
Example:
7 11_
77
area
7 11Rectangle area: 77_
![Page 67: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/67.jpg)
#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
Example:
7 11Rectangle area: 77_
7
11
rectWidth
rectHeight
77
rectArea
![Page 68: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/68.jpg)
#include <stdio.h>int computeArea(int width, int height);void outputArea(int area);int main() {
int rectWidth, rectHeight, rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeArea(rectWidth, rectHeight);outputArea(rectArea);
}int computeArea(int width, int height) {
return width * height;}void outputArea(int area) {
printf(“Rectangle area: %d\n", area);}
Example:
7 11Rectangle area: 77_
7
11
rectWidth
rectHeight
77rectArea
![Page 69: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/69.jpg)
TK1913-C ProgrammingTK1913-C Programming 6969
In a function which does not return any value, the return statement (without expression) will result in the program control being returned to the caller.
return Statementreturn Statement
![Page 70: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/70.jpg)
TK1913-C ProgrammingTK1913-C Programming 7070
Example:void displayLine(int length) {
int i = 0;while (1) {
if (i == length) {printf(“\n”);return;
}printf(“*”);i++;
}}
return Statementreturn Statement
![Page 71: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/71.jpg)
TK1913-C ProgrammingTK1913-C Programming 7171
Variables which are defined in a function can only be used within that function. This type of variables is known as local variable.
Example,
void displayLine(int lineNum) {int i;for (i = 0; i < lineNum; i++)
printf("#########\n");}
Local variables
Local VariablesLocal Variables
![Page 72: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/72.jpg)
TK1913-C ProgrammingTK1913-C Programming 7272
Local variable cannot be accessed outside the function which defines it
Local VariablesLocal Variables
![Page 73: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/73.jpg)
int computeRectArea(int, int);int main() {
int rectWidth, rectHeight, rectArea;
scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeRectArea(rectWidth, rectHeight);printf(“Rectangle area: %d", area);
}int computeRectArea(int width, int height) {
int area;area = width * height;return area;
}
Local VariablesLocal Variables
![Page 74: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/74.jpg)
int computeRectArea(int, int);int main() {
int rectWidth, rectHeight, rectArea;
scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeRectArea(rectWidth, rectHeight);printf(“Rectangle area: %d", area);
}int computeRectArea(int width, int height) {
int area;area = width * height;return area;
}
The local variable area is only for usage within function computeRectArea()-The function which defines it
Local VariablesLocal Variables
![Page 75: 1 Functions Knowledge Understanding of the structured design using function Skills Writing programs using user-defined functions and library functions.](https://reader036.fdocuments.in/reader036/viewer/2022062313/56649d575503460f94a355bf/html5/thumbnails/75.jpg)
int computeRectArea(int, int);int main() {
int rectWidth, rectHeight;int rectArea;scanf("%d%d", &rectWidth, &rectHeight);rectArea = computeRectArea(rectWidth, rectHeight);printf(“Rectangle area: %d", area);
}int computeRectArea(int width, int height) {
int area;area = width * height;return area;
}
Variable area in function computeRectArea() cannot be accessed here.
ERROR!
Local VariablesLocal Variables