Top Banner
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012
21

Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Dec 24, 2015

Download

Documents

Corey Nelson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Lecture 5: Modular Programming (functions – part 1

BJ Furman

27FEB2012

Page 2: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Learning Objectives Explain the concept of modular program design Explain the concept of a function in C Explain why functions are important in programming Explain the structure of a function

Return data type Parameters

Apply the concept of a function to a practical problem Explain how larger C programs should be structured

using .h and .c files

Page 3: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Modular Programming

Break a large problem into smaller pieces Smaller pieces sometimes called ‘modules’ or

‘subroutines’ or ‘procedures’ or functions Why?

Helps manage complexity Smaller blocks of code Easier to read

Encourages re-use of code Within a particular program or across different programs

Allows independent development of code Provides a layer of ‘abstraction’ a = sqrt(9.0);

Page 4: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Functions

The ‘building blocks’ of a C program You’ve used predefined functions already:

main() printf(), scanf(), pow()

User-defined functions Your own code In combination with predefined functions

Page 5: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Functions - Mathematical View

32)( 2

xxxf

11 is )2(

113443)2(2)2()2( 2

f

f

f(2)? isWhat

)(xf2 11X Function

Returnedvalue

Page 6: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Functions - Definition Structure Function 'header'

Return data type (if any)

Name Descriptive

Arguments (or parameter list)

Notice: data type and name

Statements Variable declaration Operations Return value (if any)

type function_name (type arg1, type arg2 )

{statements;

}

double product(double x, double y)

{ double result;

result = x * y; return result; }

A function that calculates the product of two numbers

Page 7: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Functions - Example Function prototype

Like a variable declaration Tells compiler that the function will

be defined later Helps detect program errors Note semicolon!!

Function definition See previous slide Note, NO semicolon

Function return return statement terminates

execution of the current function Control returns to the calling

function if return expression;

then value of expression is returned as the value of the function call

Only one value can be returned this way

Function call main() is the 'calling function' product() is the 'called function' Control transferred to the function

code Code in function definition is

executed

#include <stdio.h>

/* function prototype */

double product(double x, double y);

int main(){ double var1 = 3.0, var2 = 5.0; double ans;

ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans);}

/* function definition */ double product(double x, double y)

{ double result;

result = x * y; return result; }

Page 8: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Function - Practice 1

Write a function named 'sum' sums two

integers returns the sum

2 min. on your own

Share with neighbor

Steps1. Function header

• return data type• function name• argument list with data types

2. Statements in function definition• variable declaration• operations• return value

Page 9: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Function - sum()

int sum_int(int x, int y)

{ int result;

result = x + y; return result; }

Page 10: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Functions that do not return a value

Use the return type of void void my_fun( arg_list,…)

Practice Write two functions, the first prints out first

name, and the second prints out last name

Page 11: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Function - Practice 2 Program to print out two

happy :) :) or sad faces :( :(

Continuously prompts for user input:

) for happy face ( for sad face Quits if 'q' or 'Q' entered

calls two functions happy_face() sad_face()

Work in pairs Pseudocode first!! Divide tasks of writing

the two functions

Steps1. Pseudocode for program logic2. Function header

• return data type (if any)• function name• argument list with data types (if any)

3. Statements in function definition• variable declaration (if any)• operations• return value

Page 12: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Program - Faces logic Pseudocode

1. Declare and initialize variables

2. WHILE user input not equal to q AND not equal to Q1) Switch on user input to

2) Case ')‘:

call happy_face();

break;

5) Case '(‘:

call sad_face();

break;

6) Case ‘q’:

7) Case ‘Q’:break;

8) Case ‘0’:

9) Default:

re-prompt for user input

Page 13: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Program - Faces code

Page 14: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Structuring C Programs Modularization

Breaking a program up into smaller pieces: Instead of:

one_big_program.c break into groupings of header files (.h) and source code (.c) files:

module_1.h module_1.c etc.

Rationale separates the user-interface description (.h) from the nitty-

gritty details of implementation (.c) The Application Programming Interface (API), the .h file, is

distinct from the implementation, the .c file (which may already be compiled and not readily viewed)

Example: math.h from Ch can construct and test modules independently promotes re-use of code

Page 15: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Example: math.h used in Ch

See C:/ Ch / include / math.h Declaration of constants

#define M_PI 3.14159265358979323846 Declaration of macro subsitutions

#define isgreater(x, y) ((x)>(y)) Declaration of global variables (caution!) Function prototypes

extern double sin(double x); Pertinent comments

Page 16: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Review

Page 17: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Structured Programming

All programs can be written using these control structures: Sequence Decision (three structures)

IF IF-ELSE SWITCH

Repetition (three structures) WHILE DO-WHILE FOR

Page 18: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Structure of a C program

Ex. free_fall_d_vs_time.c

Page 19: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

C Code for D&D 3.15cProgrammer’s block

Pre-processor directive

Declare and initialize variables

While loop(repetition structure)

Main function (statements go between { } )

return statement

Page 20: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Arithmetic with Integers and Mixed Data Types

Arithmetic with integers Result is an integer

1+1 --> 2 4/2 --> 2 2/4 --> ? BE CAREFUL!!!

Arithmetic with mixed data types Automatic conversion of operand so that data types match

Conversion is ‘upward’ in the sizeof() sense Example in Ch

char a = 7;sizeof(a);double b=3;sizeof(b);printf("a+b == %lf and needs %d bytes\n ", a+b, sizeof(a+b));

Page 21: Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

References

Modular Programming in C http://www.icosaedro.it/c-modules.html

math.h http://www.opengroup.org/onlinepubs/007908799/xsh/math.h.html