Professional Documents
Culture Documents
Programming Concepts and Constructs (C Language) : Unit 4.8 - Function
Programming Concepts and Constructs (C Language) : Unit 4.8 - Function
Presentation 1
Revision
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.
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()
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 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
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.