Upload
shauna-webb
View
227
Download
2
Embed Size (px)
Citation preview
Functions
#include <stdio.h>
int sum(int i, int j);
main(){ int a, b, res;
a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res);}
int sum(int i, int j){ int k;
k = i + j; return k;}
Prototypedeclaration
Function definition
Function parameters
Function return value
Call/invoke a function
Arguments
Function Header
• The sum function takes two ints and returns an int.
int sum(int i, int j) { . . .}
• The function echo_line has no parameters and returns a value of type int.
int echo_line(void){ . . .}
Function Header
• The function print_stars has one parameter vol of type int and returns no value.
void print_stars(int vol){ . . .}
• The function print_prompt has no parameters and returns no value.
void print_prompt(void){ . . .}
Function Prototype Declaration
• The function print_stars has one parameter vol of type int and returns no value.
Prototype: (prototype ends with a semicolon)
void print_stars(int vol);
Function definition:
void print_stars(int vol){ while(vol-- > 0) putchar('*');}
Prototype Declaration
• Function prototype declaration should be made BEFORE the function is used.
• If the declaration is outside any function, the declaration makes it known to all the functions after the declaration in the file.
• If the declaration is inside a function A, the declared function B is known only to this function A.
Prototype Declaration Outside a function
main(){ . . .}
int sum(int a, int b);
void prt(int vol){ sum(. . . ); . . .}
float prod(float x, int y){ . . . }
The main() function does not know sum(). It cannot used sum (correctly).
The functions following the sum prototype declaration can use sum() function.
Prototype Declaration Inside a Function
main(){ . . .}
void prt(int vol){ int sum(int a, int b);
sum(. . . ); . . .}
float prod(float x, int y){ . . . }
The main() function does not know sum(). It cannot used sum (correctly).
Only the prt() function can use sum() function.
The sum() function is also not known in prod().
Define Functions
• You can define your function anywhere in a file. Before main() or after main(), both OK.
int sum(int a, int b){ …}
main(){ . . .}
Program Structure
#include <stdio.h>#include <. . . >
int f1(int a, . . .);int f2(void);int f3(. . .);
main(){ . . . }
int f1(int a, . . . ){
}
main() function call other functions.
A function can call any other functions, including itself.
Prototypes
Return Statement
• Return statement causes a termination of the current function and execution goes back to the calling function.
Return;
• return without a value.
Return exprn;
• return the value exprn to the calling function.
How the program is executed when there are
functions?
#include <stdio.h>int sum(int i, int j);
main(){ int a, b, res;
a = 1; b = 2; res = sum(a, b);
printf("%d + %d = %d\n", a, b, res);}
int sum(int i, int j){ int k; k = i + j; return k;}
a b resa, b, res have no defined values just after declaration.
a b res1 2
a b res i j1 2 1 2
res3 res is set to 3 after the
function call returns.
Copies are made to new memory locations for the parameter i and j in sum() function.
k i j
3 1 2Value 3 is sent back as the value of the expression sum(a,b) in calling function.
Example, Printing a Bar Graph
• Problem– Print a bar graph that shows
the volume of large lakes of the world. The input data are the names of the lakes and the volumes of the lakes in hundreds of cubic miles.
• Solution– We first print the name of the
lake simply by echoing the input to the output a character at a time. The bar graph is created by printing a number of stars equal to the volume of each lake in hundreds of cubic miles.
Sample Input/Output
Input dataBaikal58Superior54Tanganyika45Nyasa38Michigan26Huron21
Output dataBaikal**********************************************************
Superior******************************************************
Tanganyika*********************************************
Nyasa**************************************
Michigan**************************
Huron*********************
Input data consists of a line of name followed by a number.
C implementation without the use of
functions#include <stdio.h>
main(){ char c; int i, vol;
while( scanf(" %c", &c) != EOF) { do { printf("%c", c); scanf("%c", &c); } while( c != '\n'); printf("\n"); scanf("%d", &vol); for(i = 1; i <= vol; i++) printf("*"); printf("\n\n"); }}
Functions and Program Design
• Use functions to break a big problem into small pieces. Each small problem is solved by a function.
• For the bar graph problem we can– read and print the lake
name by echoing the input.
– Read the volume of a lake.– Print the required number
of stars.
The main() function
#include <stdio.h>#include <stdlib.h>
int echo_line(void);void print_stars(int vol);
main(){ int value;
while(echo_line() != EOF) { scanf("%d", &value); print_stars(value); } return EXIT_SUCCESS;}
Function Definitions
int echo_line(void){ char c; if (scanf(" %c", &c)==EOF) return EOF; for( ; ; ) { putchar(c); if(c == '\n') return c; c = getchar(); }}
void print_stars(int vol){ while(vol-- > 0) putchar('*'); printf("\n\n");}
Reading/Home Working
• Read Chapter 5, page 154 to 172.
• Work on Problems– Section 5.1, page 164,
exercise 1, 3, 5, 7, 9.– Section 5.3, page 171,
exercise 1, 3.
• Check your answers in the back of the textbook. Do not hand in.