You are on page 1of 7

Lecture 6

Functions.

Standard library functions

A function is a group of statements that together perform a task.

Every program you've written has contained at least one function - main().

C/C++ programs can contain many more functions that you write. The longer your program is, the better it
is to break it into several small functions. By following a building-block approach, you separate routines into their
own areas and make debugging easier because you can focus on specific problems without letting the rest of the
code get in your way.

A program with many small functions is a lot easier to maintain than one long program. When writing
separate functions, you have to manage the communication between those functions so that they can "talk" to each
other.

A structured program is a modular program that contains one function for each task the program does.
People are used to breaking large tasks into smaller, more manageable ones. Programs often need to be broken
down. Each separate function might be several lines long. The important thing to keep in mind, when writing
separate functions, is that each function should do, at most, one task. As a rule, each function shouldn't be longer
than a single screen's length. If it is any longer, the function probably does too much work and you should break it
into more functions.

When you write a function, you must start it with a name, parentheses, and surrounding braces, just like
with main(). For example, here is the familiar outline of main():

void main(){

// First, define any variables

// The body of main() goes here

When writing additional functions, keep this format in mind. You never explicitly call the main() function
because of it is a especial function. The runtime system always begins at the main() function. However, if you want
to execute (or call, in C/C++ terminology) another function in your program, you call that function by name. As
you saw with main(), the function call should be described with a return type. When a return type is void, it means
that no data is returned.
As you have to declare or define a variable before you can use it, you have to declare or define a your (user-
defined) function before you can call it.

Standard library functions

The C language standard library provides numerous built-in functions that your program can call. For
example, function strcat() to concatenate two strings, function sqrt() to find a square root, and many more
functions.

Programs combine user-defined functions with standard library functions (these functions performs often
repeated actions. So, you do not need to “reinvent a bicycle”. Just find the standard function in accordance to your
needs).

When you use a function from standard library, you have to include to your program corresponding header
file.

If you use standard library function you have just to call a function. Functions definitions and declarations
are already created by other programmers and are shared for using.

For example, when you use printf function your program should contain following statements:

#include <stdio.h> // the header file for printf function

int main(){

printf("Hello, World!"); // function calling

return 0;

This is a list of most used header files in C programming language

Header file name Description of the header file content

This is standard input/output header file in which Input/Output functions are


stdio.h declared

conio.h This is console input/output header file

string.h All string related functions are defined in this header file

stdlib.h This header file contains general functions used in C programs


math.h All maths related functions are defined in this header file

time.h This header file contains time and clock related functions

Function Declaration Using Function Prototype

A function declaration alludes to a function that is defined elsewhere and specifies what kind of value is
returned by the function, number and data types of arguments.

The general form of a function declaration, including its prototype, is as follows:

DataTypeOfReturnValue FunctionName(DataType ArgNamel, DataType ArgName2,...,DataType ArgNameN);

The purpose of using a function prototype is to help the compiler check whether the data types of arguments
passed to a function (in function call) match what the function expects. The compiler issues an error message if the
data types do not match. Although argument names, such as ArgName1, ArgName2, and so on, are optional.

Let’s discus function declarations (function prototypes) for several most used string manipulation
standard functions:

1)

Function:

strlen - Get string length

Header file: <string.h>

Returns the length of the C string str.

Function declaration (function prototype):

size_t strlen ( const char * str );

The length of a C string is determined by the terminating null-character: A C string is as long as the number
of characters between the beginning of the string and the terminating null character (without including the
terminating null character itself).

Return Value
The length of string.

2)

Function:

strcmp - Compare two strings

Header file: <string.h>

Compares the C string str1 to the C string str2.

Function declaration (function prototype):

int strcmp ( const char * str1, const char * str2 );

This function starts comparing the first character of each string. If they are equal to each other, it continues
with the following pairs until the characters differ or until a terminating null-character is reached.

Return Value

Returns a value indicating the relationship between the strings:

return value indicates

<0 the first character that does not match has a lower value in ptr1 than in ptr2

0 the contents of both strings are equal

>0 the first character that does not match has a greater value in ptr1 than in ptr2

3)

Function:

strcpy - Copy string

Header file: <string.h>

Copies the C string pointed by source into the array pointed by destination, including the terminating null
character (and stopping at that point).

Function declaration (function prototype):

char * strcpy ( char * destination, const char * source );


errno_t strcpy_s(char *restrict dest, rsize_t destsz, const char *restrict src);

To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C
string as source (including the terminating null character), and should not overlap in memory with source. destsz -
maximum number of characters to write, typically the size of the destination buffer.

Return Value

destination is returned.

4)

Function:

strcat - Concatenate strings

Header file: <string.h>

Copies the C string pointed by source into the array pointed by destination, including the terminating null
character (and stopping at that point).

Function declaration (function prototype):

char * strcat( char * destination, const char * source );

errno_t strcat_s( char *restrict dest, rsize_t destsz, const char *restrict src);

Appends a copy of the source string to the destination string. The terminating null character
in destination is overwritten by the first character of source, and a null-character is included at the end of the new
string formed by the concatenation of both in destination. destination and source shall not overlap. Destsz -
maximum number of characters to write, typically the size of the destination buffer

Return Value

destination is returned.

5)
Function:

strchr - Locate first occurrence of character in string

Header file: <string.h>

Returns a pointer to the first occurrence of character in the C string str.

Function declaration (function prototype):

char * strchr ( char * str, int character );

The terminating null-character is considered part of the C string.

Return Value

A pointer to the first occurrence of character in str.

If the character is not found, the function returns a null pointer.

Example#1:

In this piece of code, we will combine two strings first and second and place them into result string (lines 15-
16).

Then strings first and second will be compared to each other, and information about comparison will be
printed to the screen (lines 18-21).

The length of result string will be calculated and printed out in the line# 22.

The first occurrence of delimiter character ‘.’ in result string is found and shown at lines 24-25.

In the next example the line:

#include “pch.h”

is needed for Visual Studio 19. Is you will test this example in Visual Studio 17 put instead of that line another
one:

#include “stdafx.h”
The output:

You might also like