27
1 Unstructured Programming The Need for Procedures/Functions Procedural Programming • Function Declaration/Prototypes/Invocation Example Functions Function Parameters/Arguments Today’s Material

1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

Embed Size (px)

Citation preview

Page 1: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

1

• Unstructured Programming• The Need for Procedures/Functions• Procedural Programming• Function Declaration/Prototypes/Invocation• Example Functions• Function Parameters/Arguments

Today’s Material

Page 2: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

2

Unstructured Programming• So far our programs have consisted of just one

function (main), although we called library functions (printf, scanf, getchar, putchar)

• This style of programming (called unstructured programming) results in tremendous disadvantage once the program gets sufficiently large– Specifically, if the same statement sequence is needed at

different locations within the program, the sequence must be copied (duplication of code)

#include <stdio.h>

int main(){ printf(“*\n”); printf(“**\n”);} /* end-main */

Page 3: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

3

Unstructured Programming & The Need for

Procedures/Functionsint main(){ printf(“*\n”); printf(“**\n”); printf(“***\n”);

printf(“++++\n”); printf(“++++\n”); printf(“++++\n”);

printf(“*\n”); printf(“**\n”); printf(“***\n”);

printf(“++++\n”); printf(“++++\n”); printf(“++++\n”);} /* end-main */

******++++++++++++******++++++++++++

Problem: • We want to

print the following pattern on the screen

Duplic

ate

d

Duplic

ate

d

Page 4: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

4

Procedural Programming• Idea: Extract duplicating sequence of statements, combine

them into a single place and name them (called procedure, subroutine or function)

void triangle3(void){ printf(“*\n”); printf(“**\n”); printf(“***\n”);} /* end-triangle3 */

void square4(void){ printf(“++++\n”); printf(“++++\n”); printf(“++++\n”);} /* end-square4 */

int main(void){ triangle3(); square4(); triangle3(); square4();} /* end-main */

******++++++++++++******++++++++++++

int main(void){ printf(“*\n”); printf(“**\n”); printf(“***\n”);

printf(“++++\n”); printf(“++++\n”); printf(“++++\n”);

printf(“*\n”); printf(“**\n”); printf(“***\n”);

printf(“++++\n”); printf(“++++\n”); printf(“++++\n”);} /* end-main */

Page 5: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

5

Function Invocation

• To use a function, a function call is made– Called function

invocation

– The control moves to the first statement of the function

– After the steps of the function is executed, the control moves back to the next statement after the function call

void triangle3(void){ printf(“*\n”); printf(“**\n”); printf(“***\n”);} /* end-triangle3 */

void square4(void){ printf(“++++\n”); printf(“++++\n”); printf(“++++\n”);} /* end-square4 */

int main(void){ triangle3(); square4();

return 0;} /* end-main */

******++++++++++++

Page 6: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

6

What’s a Function• A function is a named sequence of

statements that performs a specific task– printf prints a message on the screen– scanf reads input from the keyboard– getchar reads one char from the keyboard– putchar prints one char on the screen– triangle3 prints a triangle using * chars– square4 prints a square using + chars– …

Page 7: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

7

Each function must contain:• A function header, a.k.a., prototype/signature• A compound statement

– comprises of the function body

returnType functionName(parameters){ declarations; statement1; statement2; … return expression;}

Function Function headerheader

Function Function bodybody Value we want Value we want

to returnto return

Page 8: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

8

Function Prototype/Signature

• consists of a return type followed by the function name followed by an optional list of parameters enclosed in parenthesis– Parameters are symbols that represent

information being passed to the function from part of the program calling the function (callee)

int Add3Ints(int a, int b, int c);

Returns an int Name Has 3 parameters

Page 9: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

9

Example Function Prototypes

(1) A function that has no parameters and returns nothingvoid F1(void);

(2) A function that has 1 int parameter and returns nothingvoid F2(int a);

(3) A function that has no parameters and returns an intint F3(void);

(4) A function that has 2 int parameters and returns an intint F4(int a, int b);

Page 10: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

10

Function with No Parameters and No Return Value (1)

void triangle4(void){ printf("*\n"); printf("**\n"); printf("***\n"); printf("****\n");} /* end-triangle4 */

int main(void){ triangle4(); triangle4(); triangle4();

return 0;} /* end-main */

******************************

void:void:this functionthis function

doesn't returndoesn't returnanythinganything

this function is this function is called three timescalled three times

Page 11: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

11

Function with No Parameters and No Return Value (2)

void sayhi(void){ printf("Hi there! How are you?\n");} /* end-sayhi */

int main(void){ sayhi(); /* function call */ sayhi(); /* called again */

return 0;} /* end-main */

Hi there! How are you?Hi there! How are you?

void:void:sayhi functionsayhi functiondoesn't returndoesn't return

anythinganything

Page 12: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

12

Function with One Parameter, No Return Value (1)

void printn(int n){ printf("n = %d\n", n);} /* end-printn */

int main(void){ printn(573); printn(-1234);

return 0;} /* end-main */

n = 573n = -1234

int n above gets 573,int n above gets 573,and the function code runsand the function code runs

int n above gets -1234,int n above gets -1234,and the function code runsand the function code runs

Page 13: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

13

Function with One Parameter, No Return Value (2)

void powers(double x){ double x2 = x * x; double x6 = x2 * x2 * x2;

printf("x = %.6lf\n", x); printf("x^2 = %.6lf\n", x2); printf("x^6 = %.6lf\n", x6);} /* end-powers */

int main(void){ powers(1.5); printf("--------\n"); powers(0.11);

return 0;} /* end-main */

x = 1.500000x^2 = 2.250000x^6 = 11.390625--------x = 0.110000x^2 = 0.012100x^6 = 0.000002

double x above gets 1.5,double x above gets 1.5,and the function code runsand the function code runs

double x above gets 0.11,double x above gets 0.11,and the function code runsand the function code runs

Page 14: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

14

Function with One Parameter, No Return Value (3)

void printstars(int n){ int i;

for(i = 0; i < n; i++) printf("*"); printf("\n");} /* end-printstars */

int main(void){ printstars(1); printstars(2); printstars(3); printstars(4);

return 0;} /* end-main */

**********

int n above gets 1,int n above gets 1,and the function code runsand the function code runs

int n above gets 2,int n above gets 2,and the function code runsand the function code runs

Page 15: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

15

Equivalent Code

void printstars(int n){ int i;

for(i = 0; i < n; i++) printf("*"); printf("\n");} /* end-printstars */

int main(void){ int i;

for(i = 1; i <= 4; i++) printstars(i);

return 0;} /* end-main */

**********

int n above gets value of i,int n above gets value of i,and the function code runsand the function code runs

Page 16: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

16

Functions with One Parameter and a Return Value

/* Returns x ^ 6 */double power6(double x){ double x2 = x * x;

return x2 * x2 * x2;} /* end-power6 */

int main(void){ double p6 = power6(1.5); printf("%.6lf", p6);

return 0;} /* end-main */

/* Finds sum 1 + ... + n */int sum(int n){ return n * (n + 1) / 2;} /* end-sum */

int main(void){ printf("%d", sum(5));

return 0;} /* end-main */

Page 17: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

17

Function with Two Parameters and No Return Value

void printmany(int n, char c){ int i;

for(i = 0; i < n; i++) printf("%c", c); printf("\n");} /* end-printmany */

int main(void){ printmany(5, '*'); printmany(10, '-'); printmany(15, '=');

return 0;} /* end-main */

*****----------===============

Page 18: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

18

Function Parameters & Arguments

• Parameters are identifiers that appear in function declarations, i.e., function prototypes/signatures– Parameters are just dummy names that represent the value to

be supplied to the function

• Arguments are expressions that appear in function calls/* a, b and c are parameters */int Add(int a, int b, int c){ return a+b+c;} /* end-Add */

void main(void){ int x = 2; int y;

/* x, 3 and x*2 are arguments */ y = Add(x, 3, x*2);} /* end-main */

• Notice that arguments to functions can be arbitrary expressions as in the given example– x– 3– x*2

Page 19: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

19

Argument Passing Convention• In C, all arguments are passed-by-value

– That is, all arguments are evaluated before the function call, and the values of the arguments are copied to the corresponding parameter

– Changes made to the parameter during the execution of the function do not affect the value of the argument

Page 20: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

20

Pass-by-Value Exampleint F(int a, int b, int c){ a += b+c; /* change parameter ‘a’ inside the function */

return a;} /* end-F */

void main(void){ int x = 2, y = 3, z = 4; int t;

t = F(x, y, z); printf(“x: %d, y: %d, z: %d, t: %d\n”, x, y, z, t);} /* end-main */

/* will print x: 2, y: 3, z: 4, t: 9 *//* Although parameter ‘a’ is changed inside the function, the corresponding argument ‘x’ is not changed. Isn’t it then possible to change the value of arguments? */

Page 21: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

21

Changing Arguments inside Functions

void decompose(float x, int int_part, float frac_part){ int_part = (int)x; frac_part = x – int_part;} /* end-decompose */

void main(){ int i = 0; float f = 0.0;

decompose(2.35, i, f); printf(“int_part: %d, frac_part: %.2f\n”, i, f);} /* end-main*//* Prints: int_part: 0, frac_part: 0.00 */

• You want to write a function that returns the integer and fractional parts of a float– f = 2.35 int_part: 2, frac_part: 0.35– Here is a function that supposedly implements this

Page 22: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

22

Changing Arguments inside Functions

• Problem: We want to change the value of an argument inside a function– Recall that in C all arguments are passed-

by-value

• How would you solve this problem? – We need to cover pointers to answer this

question

Page 23: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

23

Placement of Functions (1)

preprocessor directivesnamed constants

function definitions...

int main(){variable declarations;

executable statements;

return value;} /* end-main */

#include <stdio.h>

int abs(int a){ return a < 0 ? -a : a;} /* end-abs */

int main(){ int n = -25;

printf("abs(%d) = %d\n", n, abs(n)); return 0;} /* end-main */

Page 24: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

24

Placement of Functions (2)

preprocessor directivesnamed constants

function prototypes

int main(){variable declarations;

executable statements;

return value;} /* end-main */

function definitions

#include <stdio.h>

int abs(int);

int main(){ int n = -25;

printf("abs(%d) = %d\n", n, abs(n)); return 0;} /* end-main */

int abs(int a){ return a < 0 ? -a : a;} /* end-abs */

Page 25: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

25

Reusability & Modular Programming

• Often we want to group a set of functions together into a module (a.k.a., a unit or library)– We implement the functions and test them for

correctness– We can then use these functions in other

programs as necessary– This style of programming is called modular

programming– Clearly modular programming leads to reusability

Page 26: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

26

Modular Programming Example• We will implement a simple library of functions

– lib.h: contains function prototypes only• int AvgOf3Ints(int a, int b, int c);• int AvgOf3Doubles(double a, double b, double c);• int MinOf3Ints(int a, int b, int c);• int MaxOf3Ints(int a, int b, int c);

– lib.c: contains function implementations– main.c: Just the main function that calls our library functions to test for correctness

• Notice that all standard C library functions are implemented this way– printf, scanf,getchar, putchar, …– stdio.h contains function prototypes, libc.a implementations

Page 27: 1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function

27

Advantages of Procedural/Modular Programming

• Avoids duplication of code• Modularity

– We can divide our programs into small code snippets (pieces) that are easier to understand and modify

• Correctness & Reusability– Once we implement & test our procedures, they

will produce correct results every time we call them

– So we can now use these procedures over and over again in different programs

• printf, scanf, getchar, putchar, …