Sunteți pe pagina 1din 22

Programming Concepts and Constructs (C Language)

Unit 4.8 - Function

Presentation 1

Revision

1. Two-dimensional arrays are initialised __________________. a. Row-wise. b. Column-wise.


2. In x[i][j], i refers to ______________ and j refers to __________________.

Objectives
At the end of this presentation, you will be able to: List the advantages of functions List the function components List the types of functions Accept data from the user and display data on the screen using standard input/output functions

Functions
Is a block of statements that performs a specific task.

Advantages of Using Function


Reusability
Function Portability Modularity Easy to Debug Easy to Modify and Extend

Function Components
Function Header - Specifies the function name along with parameters. Function Body - Contains the actual code of the function. Return Statement - Returns a value to the main program.

Types of Function
The two types of functions in C language are:
Built-in functions User-defined functions

Built-in Functions
Are pre-defined functions that are part of the C language.
Built-in functions are also called as library functions. Example

printf() scanf()

Built-in Functions
The code for these functions are already defined by the C language. To use a particular function, you must include the respective header file.

User-Defined Functions
Are the functions written by the users for performing a specific task. Example

main()

Header File - stdio.h


This is the standard input-output header file.
The functions in this header file are used for accepting the input data from the user and displaying the processed data on the screen.

Functions in stdio.h
Function : getchar() Description : Accepts a single character from the user. Syntax : getchar();

Functions in stdio.h
Function : putchar() Description : Displays a single character on the screen. Syntax : putchar(<variable name>);

Hands-On!
Program D8_1.C will accept and display a character on the screen.

Functions in stdio.h
Function : gets() Description : Accepts a string from the user. Syntax : gets(<variable name>);

Functions in stdio.h
Function : puts() Description : Displays the string on the screen. Syntax : puts(<variable name>);

Hands-On!
Program D8_2.C will accept and display a string on the screen.

Summary
In this presentation, you learnt the following: A function is a block of statements that performs a specific task. Function components:

Function Header

Function Body
Return Statement

Summary
Types of functions available in C are:

Built-in functions User-defined functions

Built-in functions are pre-defined functions that are part of the C language.

Standard input-output functions can be used to accept input from the user and display the same.

Assignment

1. What are the two types of functions available in C language?


2. Which of the following functions is more appropriate for reading multi-word strings?

a) getchar() b) putchar() c) gets() d) puts()

Lab Exercise
1. Write a program to accept characters from the user. The program must accept the characters till the user presses the Enter key.

Lab Exercise
2. Write a program to count the number of blank spaces in the input string.

S-ar putea să vă placă și