0% found this document useful (0 votes)
12 views4 pages

Lesson 12 - Function - Compatibility Mode

The document discusses functions in C programming. It covers function definitions, prototypes, calling functions by value and reference, and scope rules. Specifically, it defines functions as program modules that perform operations and return results. Functions are defined with a return type, name, and parameter list. Function prototypes declare the return type and parameters but not the body. Functions are called by either passing arguments by value, where copies are used, or by reference, where the original arguments can be modified. Scope rules determine where identifiers are accessible within a program.

Uploaded by

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

Lesson 12 - Function - Compatibility Mode

The document discusses functions in C programming. It covers function definitions, prototypes, calling functions by value and reference, and scope rules. Specifically, it defines functions as program modules that perform operations and return results. Functions are defined with a return type, name, and parameter list. Function prototypes declare the return type and parameters but not the body. Functions are called by either passing arguments by value, where copies are used, or by reference, where the original arguments can be modified. Scope rules determine where identifiers are accessible within a program.

Uploaded by

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

FUNCTIONS IN C

Introduction

Nguyen Thi Thu Huong - SoICT -HUST


FUNCTIONS Program Modules in C

Functions

Nguyen Thi Thu Huong Function Definitions


Department of Computer Science
School of Information and Communication Technology Function Prototypes
11 Hanoi University of Science and Technology
CallingFunctions: Call by Value and Call by Reference
2
Scope Rules
Nguyen Thi Thu Huong - SoICT -HUST

1 2

INTRODUCTION: DIVIDE AND


CONQUER PROGRAM MODULES IN C
Programs written by combining user-
defined functions with library functions
Nguyen Thi Thu Huong - SoICT -HUST

Nguyen Thi Thu Huong - SoICT -HUST


Construct a program from smaller pieces or components
C standard library has a wide variety
Each piece more manageable than the original program
of functions
Makes programmer's job easier -
avoid reinventing the wheel

3 4

3 4

PROGRAM MODULES IN C FUNCTION DEFINITION FORMAT


Function calls
return-value-type function-name( parameter-list )
Invoking functions {
Nguyen Thi Thu Huong - SoICT -HUST

Nguyen Thi Thu Huong - SoICT -HUST

declarations and statements


Provide function name and }

arguments (data) Function-name: any valid identifier


Function performs operations or Return-value-type: data type of the result (default
int)
manipulations void - function returns nothing
Function returns results Parameter-list: comma separated list, declares
parameters (default int)

5 6

5 6

1
FUNCTION DEFINITIONS FUNCTION PROTOTYPES
Declarations and statements: function body (block) Function prototype
Variables can be declared inside blocks (can be nested) Function name
Function can not be defined inside another function Parameters - what the function takes in

Nguyen Thi Thu Huong - SoICT -HUST

Nguyen Thi Thu Huong - SoICT -HUST


Returning control Return type - data type function returns (default int)
If nothing returned Used to validate functions
return; Prototype only needed if function definition comes after use in
program
or, until reaches right brace int maximum( int, int, int );
If something returned Takes in 3 ints
return expression; Returns an int
Promotion rules and conversions
Converting to lower types can lead to errors

7 8

7 8

CALLING FUNCTIONS: CALL BY


VALUE AND CALL BY REFERENCE SCOPE RULES
File scope
Used when invoking functions Identifier defined outside functions, known in all
Nguyen Thi Thu Huong - SoICT -HUST

Nguyen Thi Thu Huong - SoICT -HUST


Call by value functions
Copy of argument passed to function Used for global variables, function definitions,
Changes in function do not effect original function prototypes
Use when function does not need to modify argument
Function scope
Avoids accidental changes
Call by reference
Can only be referenced inside a function body
Passes original argument Used only for labels (start: , case: , etc.)
Changes in function effect original
Only used with trusted functions
For now, we focus on call by value
9 10

9 10

• int A;
• void main() VARIABLE
SCOPE RULES
{ A = 1; SCOPE
Block scope myProc();
printf ( "A = %d\n", A);
Identifier declared inside a block }
Nguyen Thi Thu Huong - SoICT -HUST

Nguyen Thi Thu Huong - SoICT -HUST

Block scope begins at definition, ends at right brace


Used for variables, function parameters (local void myProc() Printout:
variables of function) { int A = 2;
while( A==2 ) --------------
Outer blocks "hidden" from inner blocks if there is a {
variable with the same name in the inner block int A = 3; A= 3
Function prototype scope printf ( "A = %d\n", A);
A= 2
break;
Used for identifiers in parameter list
}
printf ( "A = %d\n", A);
A= 1
}
...
11 12

11 12

2
DATA STRUCTURES (STRUCT)

Arrays require that all elements be of the same

Nguyen Thi Thu Huong - SoICT -HUST


data type.
It is necessary to group information of different
data types.
Materials list for a product: name for each item, part
number, dimensions, weight, and cost.
Nguyen Thi Thu Huong The data structures that can store combinations of
U 6: S
Department
NIT of Computer
TRUCTURESScience different data types are called structs.
131
Faculty of Information Technology
Hanoi University of Technology

14

Nguyen Thi Thu Huong - SoICT -HUST

13 14

STRUCTURES (STRUCT) DECLARING STRUCTURES (STRUCT)


A struct is a derived data type composed of Does Not Reserve Space Reserves Space
members that are each fundamental or derived struct my_example
data types.
Nguyen Thi Thu Huong - SoICT -HUST

Nguyen Thi Thu Huong - SoICT -HUST


struct my_example {
{ int label;
A single struct would store the data for one
int label; char letter;
object. An array of structs would store the data
for several objects. char name[20];
char letter;
} mystruct ;
char name[20];
A struct can be defined in several ways };
/* The name "my_example" is
called a structure tag */
15 16

15 16

USER DEFINED DATA TYPES (TYPEDEF) TYPEDEF & STRUCT


The C language provides a facility called typedef Often, typedef is used in combination with struct to
for creating synonyms for previously defined data declare a synonym (or an alias) for a structure:
type names. For example, the declaration:
Nguyen Thi Thu Huong - SoICT -HUST

Nguyen Thi Thu Huong - SoICT -HUST

typedef struct /* Define a structure */


typedef int Length; {
int label ;
makes the name Length a synonym (or alias) for char letter;
the data type int. char name[20] ;
The data “type” name Length can now be used in } Some_name ; /* The "alias" is Some_name */
declarations in exactly the same way that the
data type int can be used: Some_name mystruct ; /* Create a struct variable */
17 18
Length a, b, len ;
Length numbers[10] ;

17 18

3
ACCESSING STRUCT MEMBERS
SAMPLE PROGRAM WITH STRUCTS
Individual members of a struct variable may be /* This program illustrates creating structs and
accessed using the structure member operator (the then declaring and using struct variables.
Note that struct personal is an included data

Nguyen Thi Thu Huong - SoICT -HUST

Nguyen Thi Thu Huong - SoICT -HUST


dot, “.”): type in struct "identity".
mystruct.letter ; */
#include <stdio.h>
struct personal //Create a struct but don’t reserve space.
Or , if a pointer to the struct has been declared and { long id;
float gpa;
initialized
};
Some_name *myptr = &mystruct ; struct identity //Create a second struct that includes the first
one.
by using the structure pointer operator (the “->“): { char name[30];
myptr -> letter ; struct personal person;
};
which could also be written as: 19 20

(*myptr).letter ;

19 20

SAMPLE PROGRAM WITH STRUCTS (CONT.)


int main ( )
{
struct identity js = {"Joe Smith"}, *ptr = &js ;
Nguyen Thi Thu Huong - SoICT -HUST

js.person.id = 123456789 ;
js.person.gpa = 3.4 ;
printf ("%s %ld %f\n", js.name, js.person.id,
js.person.gpa) ;
printf ("%s %ld %f\n", ptr->name, ptr->person.id,
ptr->person.gpa) ;
}

21

21

You might also like