0% found this document useful (0 votes)
35 views49 pages

Chapter 4 Functions

The document discusses functions in C programming. It covers: 1) Functions allow breaking programs into smaller, more manageable pieces called top-down design. A C program consists of one or more functions, with one function called main that is the program entry point. 2) Functions receive parameters, operate on them, and may return a single value or have side effects like changing global variables. Functions can be called for their returned value, side effects, or both. 3) The basics of defining functions including return types, parameters, and calling functions are covered. Functions are defined with a return type, name, parameters, and body. Functions are called by name and passing actual parameters.

Uploaded by

Habibur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views49 pages

Chapter 4 Functions

The document discusses functions in C programming. It covers: 1) Functions allow breaking programs into smaller, more manageable pieces called top-down design. A C program consists of one or more functions, with one function called main that is the program entry point. 2) Functions receive parameters, operate on them, and may return a single value or have side effects like changing global variables. Functions can be called for their returned value, side effects, or both. 3) The basics of defining functions including return types, parameters, and calling functions are covered. Functions are defined with a return type, name, parameters, and body. Functions are called by name and passing actual parameters.

Uploaded by

Habibur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 49

The C Programming Language

Chapter 4 Functions

Chapter 4 Functions
4-1 Functions in C

In C, the idea of top–down design is done using


functions. A C program is made of one or more
functions, one and only one of which must be named
main.

In general, the purpose of a function is to receive zero


or more pieces of data, operate on them, and return at
most one piece of data. At the same time, a function can
have a side effect. A function side effect is an action
that results in a change in the state of the program.
2
Note
In C, a program is made of one or more
functions, one and only one of which must be
called main.

The execution of the program always starts


with main, but it can call other functions
to do some part of the job.

3
1 Overview of functions 2 Definition of function

3 Return statement 4 Function parameter

5 Function call 6 Internal / external


functions

7 Local and global variables 8 Function application


Functions in Mathematics
Car manufacturers make cars
carriage

Engine
wheel

chassis
Division of labor and
cooperation
A B

C D
The form of function definition
Type specifier: used to identify the return value type of a function 。 Eg:
int ,double …
Function name: "see name to know meaning, commonly used to simplify"
Parameter list: a list of various types of variables. The parameters are
separated by commas.
FIGURE 4-3 Structure Chart for a C Program
12
FIGURE 4-4 Function Concept
13
Note
A function in C can have a return value,
a side effect, or both.

The side effect occurs before the value is


returned. The function’s value is the value in
the expression of
the return statement. A function can be
called
for its value, its side effect, or both.

14
PROGRAMSample
4-1 Program with Subfunction

15
PROGRAMSample
4-1 Program with Subfunction

16
PROGRAMSample
4-1 Program with Subfunction

17
User-Defined Functions
Like every other object in C, functions
must be both declared and defined. The
function declaration gives the whole
picture of the function that needs to be
defined later. The function definition
contains the code for a function.
Topics discussed in this section:
Basic Function Designs
Function Definition
Function Declaration
The Function Call
18
Chapter 4 Fuctions
4.2 Basics of Function

4.2 Basics of Function


General Form:
return_type function_name(formal_parameters_list)
{
declarations
statements
}
formal_parameters_list :
type var1, type var2, type var3, ..., type varn
e.g.:
int max(int x, int y) A special function
{ --null function
int z;
z = x > y ? x : y; dummy() { }
return(z);
}
PROGRAMvoid
4-2 Function with a Parameter

20
PROGRAMvoid
4-2 Function with a Parameter

21
PROGRAMvoid
4-2 Function with a Parameter

22
FIGURE 4-7 Non-void Function without Parameters
23
FIGURE 4-8 Calling a Function That Returns a Value
24
PROGRAM 4-3
Read a Number and Square It

25
PROGRAM 4-3
Read a Number and Square It

26
PROGRAM 4-3
Read a Number and Square It

27
PROGRAM 4-3
Read a Number and Square It

28
Chapter 4 Fuctions
4.3 Function Call

#include<stdio.h>
main()
{
float add(float x, float y); /* prototype
(declaration of add) */
float a, b, c;
scanf("%f,%f", &a, &b);
c = add(a, b);
printf("sum is %f\n", c);
}
float add(float x, float y) /*definition of add*/
{
float z;
z = x + y;
return(z);
}
Chapter 4 Fuctions
4.3 Function Call

Example: Digital pyramid


1
#include<stdio.h>
2 2
int main()
{ 3 3 3
1
void pyramid(int n);
2 2
pyramid(15); 3 3 3
return 0; 4 4 4 4
} 5 5 5 5 5
6 6 6 6 6 6
void pyramid ( int n ) 7 7 7 7 7 7 7
{ 1 8 8 8 8 8 8 8 8
22 9 9 9 9 9 9 9 9 9
int i,j; 333
for(i=1; i<=n; i++){ 4444
55555
for(j=1; j<=n-i; j++) 666666
7777777
printf(" "); 88888888
for(j=1; j<=i; j++) 999999999
aaaaaaaaaa
printf("%0x ", i); bbbbbbbbbbb
ccc cccc ccccc
printf("\n"); ddddddddddddd
} eeee ee eeee eeee
f f f f f f f f f f f f f f f
}
FIGURE 4-9 Function Definition
31
FIGURE 4-10 Function Return Statements
32
FIGURE 4-11 Function Local Variables
33
Note
Formal and Actual Parameters
Formal parameters are variables that are
declared in the header of the function
definition.

Actual parameters are the expressions in the


calling statement.

Formal and actual parameters must match


exactly in type, order, and number.

Their names, however, do not need to match.

34
FIGURE 4-12 Parts of a Function Call
35
FIGURE 4-13 Examples of Function Calls
36
PROGRAM 4-4
Print Least Significant Digit

37
PROGRAM 4-4
Print Least Significant Digit

38
PROGRAM 4-4
Print Least Significant Digit

39
Chapter 4 Fuctions
4.4 Parameters and Returning Value of Function

4.4 Parameters and


Returning Value of Function
Formal Parameters & Actual Parameter
Function Definition: Formal parameter
Function Call: Actual parameter
#include <stdio.h> Formal parameter
main()
{ Actual parameter int max(int x, int y)
int a, b, c; {
int max(int x, int y); int z;
scanf("%d%,%d", &a, &b); z = x > y ? x : y;
c = max(a, b); return(z);
printf("Max is %d\n", c); }
}
Chapter 4 Fuctions
4.4 Parameters and Returning Value of Function

All function parameters are passed by value.


The called function is given the values of its
parameters in temporary variables rather than
the originals, and it cannot directly alter a
variable in the calling function.
Actual parameters can be constants,
variables, and expressions. For example,
max(3, a+b) .
Any formal parameter’s type must be defined.
Formal and actual parameters of one function
must have the same type.
Chapter 4 Fuctions
4.4 Parameters and Returning Value of Function

Example:
#include <stdio.h>
void swap(int x, int y); void swap(int x, int y)
main() {
{ int t;
int a=10, b=200; t=x;
swap(a,b); x=y;
printf(“a=%d\tb=%d\n", a,b); y=t;
} }

 what is the program’s


output result?
Chapter 4 Fuctions
4.4 Parameters and Returning Value of Function

If the type of function value doesn’t match


that of the expression value of return
statement, take the type of function.

If there is no return statement included in


the function, the value of the function is
indefinite.

void means “no return value”, or “dump


type”.
FIGURE 4-14 Design for Add Two Digits
44
PROGRAM 4-5
Add Two Digits

45
PROGRAM 4-5
Add Two Digits

46
PROGRAM 4-5
Add Two Digits

47
PROGRAM 4-5
Add Two Digits

48
Exercises

P.71 4-1
P.88 4-13

You might also like