1 lecture 6 functions c standard libraries invocation definition call-by-value prototype ...

23
1 Lecture 6 Functions C Standard Libraries Invocation Definition Call-by-value Prototype Multi-file program Macros Readings: Chapter 4 Section 1 to 12

Post on 21-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

1

Lecture 6 Functions

C Standard Libraries Invocation Definition Call-by-value Prototype

Multi-file program Macros Readings: Chapter 4 Section 1 to 12

Page 2: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

2

Program Components

A C program is made up of one or more functions, one of them being main() which is the starting point of program execution

Typically, some functions are defined by the programmer and the others are predefined functions provided in the C standard libraries or other libraries.

Page 3: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

3

The C standard libraries provide functions for:

Input/output, math calculation, character/string processing, error-checking…

Related library functions are grouped and can be referenced by including the corresponding header file into the source program during preprocessing

Header files for ANSI C’s standard library<assert.h> <float.h> <math.h> <stdarg.h>

<stdlib.h> <ctype.h> <limits.h> <setjmp.h>

<stddef.h> <string.h> <errno.h> <locale.h>

<signal.h> <stdio.h> <time.h>

Page 4: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

4

Functions

Why functions? Natural for top-down design approach Enhance software reusability Avoid repeating code

main

worker3worker2worker1

worker3worker4

Page 5: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

5

Function Invocation

When a function is called (invoked), program control is passed to that function.

When that function ends, program control is returned to the statement immediately after the function call.

Syntax:function_name ( argument_list )

Page 6: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

6

Calling Predefined Functions

#include <stdio.h>

#include <math.h>

void main()

{

float area;

printf(“Enter the area of a square: “);

scanf(“%f”, &area);

printf(“The square has perimeter: %f”,

4*sqrt(area));

}

Page 7: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

7

Another example#include <stdio.h>#include <stdlib.h>

int main() { int i=0, n; printf(“No. of random integers you want to see? ”); scanf(“%d”, &n); while (i < n) { if (i++ % 6 == 0) /* 6 numbers (max.) per line */

printf("\n"); printf(“%9d”, rand()); /* call rand() in stdlib */}

printf(“\n”); return 0;}

Page 8: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

8

Programmer-defined functions

You can define your own functions and then call them as needed.

Either the function definition or function prototype must appear before the function calls. Function prototype describes how the function is

invoked (interface) Function definition describes how the function

computes the return value (implementation) A function definition cannot be nested within

another function definition.

Page 9: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

9

Function Definition

Syntax:type function_name( parameter_list )

{

sequence_of_statements

}

Everything before the ‘{’ comprises the function header; the rest constitutes the function body

Page 10: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

10

Example#include <stdio.h>

int powerOfTwo(int n){ int power=1; for (; n>0; n--) power *= 2; return power;}

void main(){ int i, result; printf(“Enter a non-negative integer: “); scanf(“%d”, &i); result = powerofTwo(i); printf(“The %d-th power of 2 is %d\n”, i, result); return;}

Page 11: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

11

Invocation & Call-by-Value When a C function is invoked, the arguments

within the parentheses are passed using a call-by-value mechanism, which means that each argument is evaluated, and its value is used locally in place of the corresponding formal parameter

Example (previous page): Suppose user input 7 for the value of i. The

argument received by function powerOfTwo is 7. At the end, power and n are destroyed. They are

called local variables (covered next time.) Variable result will get the value 128. Variable i is unchanged

Page 12: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

12

Call-by-value (cont’)

powern

resulti

112870

1287

Destroyed afterfunction invocation

Page 13: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

13

The return Statement

When a return is encountered, program control goes back to the calling function the value of the expression after the keyword return is sent back to the calling function

The return value of a function will be converted, if necessary, to the type of function as specified in the header to the function definition

Syntax:return expression;return;

Page 14: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

14

Example of return#include <stdio.h>

int min(int a, int b) { if (a<b) return a; return b;}

int main(void) {int j, k, m;

printf("Input two integers: "); scanf("%d%d", &j, &k); m = min(j, k); printf("\nOf the two values %d and %d, " "the minumum is %d.\n\n", j, k, m); return 0;}

Page 15: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

15

The void data type When a function does not return any value, the

type of function is void, e.g, :void err_msg(int i){ switch (i) { case 1: printf(“invalid user”); break; case 2: printf(“input too large”); break; default: printf(“system error”); } return; /* optional */}

When a function does not take any argument, the parameter list is void, e.g. :e.g.: int fabc(void) or int fabc()

Page 16: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

16

Function Prototype

A function prototype tells the compiler the number and type of arguments that are to be passed to the function and the type of the value that is to be returned by the function

Syntax: type function_name(parameter_type_list);

Ortype function_name(parameter_list);

Page 17: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

17

Example of function prototype

#include <stdio.h>int powerOfTwo(int n);

void main(){ int i; printf(“Enter a non-negative integer: “); scanf(“%d”, &i); printf(“The %d-th power of 2 is %d\n”, i, powerOfTwo(i)); return;}

int powerOfTwo(int n){ int power=1; for (; n>0; n--) power *= 2; return power;}

Page 18: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

18

A multi-file C program

pgm.h

#include <stdio.h>

#define N 3

void fct1(int);

void fct2(void);

void prn_info(void);

Page 19: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

19

A multi-file C program (cont’d)main.c

#include "pgm.h"

int main(void) { char ans;

printf("\nDo you want more information? "); scanf("%c", &ans); if (ans == 'y' || ans == 'Y') prn_info();

fct1(N);

printf("\nBye!\n\n"); return 0;}

Page 20: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

20

A multi-file C program (cont’d)prn.c

#include "pgm.h"

void prn_info(void) {

printf(”\nUsage: pgm\n\n"

"This program illustrates how one can write\n"

"a program in more than one file. In this\n"

"example, we have a single .h file that gets\n"

"included at the top of our three .c files.\n"

"Thus the .h file acts as the \"glue\"\n"

"that binds the program together.\n"

"\n”);

}

Page 21: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

21

A multi-file C program (cont’d)

fct.c

#include “pgm.h”

void fct1(int n) {

int i;

printf(“Hello from fct1()\n”);

for (i = 0; i < n; ++i)

fct2();

}

void fct2(void) {

printf(“ Hello from fct2()\n”);

}

Page 22: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

22

How are the files related?

Page 23: 1 Lecture 6  Functions  C Standard Libraries  Invocation  Definition  Call-by-value  Prototype  Multi-file program  Macros  Readings: Chapter

23

Macros

Macros are defined with #define

Problem: 11 is printed instead of 25 Solution: #define SQUARE(x) (x)*(x)

#define SQUARE(x) x*x

#include<stdio.h>

int main(void)

{

printf("Result is %d\n", SQUARE(2+3));

return 0;

}