0% found this document useful (0 votes)
9 views7 pages

Functions 001

The document discusses functions in C programming, detailing their structure, usage, and advantages such as code reusability and modular design. It covers various aspects including function declarations, calling methods (call by value and call by reference), recursion, and dynamic memory allocation. Additionally, it provides examples and explanations of standard C functions and libraries for input/output operations.
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)
9 views7 pages

Functions 001

The document discusses functions in C programming, detailing their structure, usage, and advantages such as code reusability and modular design. It covers various aspects including function declarations, calling methods (call by value and call by reference), recursion, and dynamic memory allocation. Additionally, it provides examples and explanations of standard C functions and libraries for input/output operations.
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/ 7

Thursday, December 5, 2024

Functions

• In C, we can divide a large program into the basic Building Blocks known as Function

Unit-4 : Function and • The Function contains the set of programming statements enclosed by { }
• The Function is also known as Procedure or Sub-Routine in other programming

Dynamic Memory Allocation languages


• A Function is a complete and independent program which is used (or invoked) by the
main program or other subprograms

Srinivas V, +91 99499 10792 2 Srinivas V, +91 99499 10792 5

Unit-4 : Function and Dynamic Memory Allocation Functions

• Functions: Designing structured programs, Declaring a function, • A function has Declaration:

• declaration
Signature of a function, Parameters and Return Type of a [ data type] function name (argument list);
• call
function, passing parameters to functions, call by value, Passing • definition
Call :

arrays to functions, passing pointers to functions, idea of call by [variable] = function name (value list);

reference, Some C standard functions and libraries. Definition:

• Recursion: Simple programs, such as Finding Factorial, Fibonacci [ data type] function name (argument list)
argument declaration
series etc., Limitations of Recursive functions {
local variable declarations;
• Dynamic memory allocation: Allocating and freeing memory, statements;
[return expression]
Allocating memory for arrays of different data types. }

Srinivas V, +91 99499 10792 3 Srinivas V, +91 99499 10792 6

Ex: Basic Calculator application using Functions

2 1

3
Functions

Srinivas V, +91 99499 10792 4 Srinivas V, +91 99499 10792 7


Thursday, December 5, 2024

Advantages Function without argument and with return value

• It avoids rewriting same logic / code again and again in a program


• Can be called any number of times in a program and from any place in a program
• Reusability is the main achievement of Functions

Srinivas V, +91 99499 10792 8 Srinivas V, +91 99499 10792 11

Function with Arguments and without return value

Different ways of calling a Function

Srinivas V, +91 99499 10792 9 Srinivas V, +91 99499 10792 12

Without Arguments and Without Return Value Function with Arguments and with Return Value

Srinivas V, +91 99499 10792 10 Srinivas V, +91 99499 10792 13


Thursday, December 5, 2024

Q. WACP to find sum of elements of an array using a Function?

Arrays to Function

Srinivas V, +91 99499 10792 14 Srinivas V, +91 99499 10792 17

Passing an Array to a Function Passing 2-D array to function

• Pass an array to a Function say fun(), it is always treated as a pointer by fun().


Pointer to array Length of array

It is compulsory
to pass length
Pointer takes
base address
of the array

Srinivas V, +91 99499 10792 15 Srinivas V, +91 99499 10792 18

Ex: Passing Array to Function

Pointer to Function

Srinivas V, +91 99499 10792 16 Srinivas V, +91 99499 10792 19


Thursday, December 5, 2024

Passing a Pointer to a Function

• As an argument, a pointer is passed instead of a variable and its address is passed


instead of its value.
• As a result, any change made by the function using the pointer is permanently stored at
the address of the passed variable.
• In C, this is referred to as call by reference.

Recursion

Srinivas V, +91 99499 10792 20 Srinivas V, +91 99499 10792 23

Ex: Passing a Pointer to a Function Recursion

• Generally, iterative solutions are more efficient than recursion since function call is always
overhead.
• Any problem that can be solved recursively, can also be solved iteratively.
• However, some problems are best suited to be solved by the recursion, for example, tower of
Hanoi, Fibonacci series, factorial finding, etc.

Srinivas V, +91 99499 10792 21 Srinivas V, +91 99499 10792 24

Functions continued… Recursion

• Types of Function Call


• Call by Value
Call by Value :

[ variable] = function name (val1, val2 …);

• Call by Reference Call by Reference:

[variable] = function name (add1, add2, …);

Srinivas V, +91 99499 10792 22 Srinivas V, +91 99499 10792 25


Thursday, December 5, 2024

Recursion – First 50 Natural numbers using Recursion Recursion – Prime or NOT

Srinivas V, +91 99499 10792 26 Srinivas V, +91 99499 10792 29

Flow Chart Recursion – Factorial of a number

Srinivas V, +91 99499 10792 27 Srinivas V, +91 99499 10792 30

Recursion – SUM of numbers Recursion – Fibonacci series

Srinivas V, +91 99499 10792 28 Srinivas V, +91 99499 10792 31


Thursday, December 5, 2024

Recursion – Fibonacci series Some C standard Functions and Libraries

Function Library / Header File Description

printf() stdio.h To print on output console

scanf() stdio.h Used to take user input

int abs(int n) stdlib.h Absolute value of integer value n

void *malloc(size_t size) stdlib.h Reserves a block of storage

int getchar(void) stdio.h Reads a single character from std input

double log(double x) math.h Natural Logarithm of x

Srinivas V, +91 99499 10792 32 Srinivas V, +91 99499 10792 35

Recursion – Fibonacci series Formatted Input Function – scanf()

• The function scanf() is used for formatted input from standard input and provides many
of the conversion facilities of the function

• Syntax
scanf (format, num1, num2,……);
• The function scnaf() reads and converts characters from the standards input depending
on the format specification string and stores the input in memory locations represented
by the other arguments (num1, num2,….)
• Ex:
scanf(“ %c %d”,&Name, &Roll No);

Srinivas V, +91 99499 10792 33 Srinivas V, +91 99499 10792 36

Formatted Output Function

• Formatted Output - printf()


• formatted output to standard output based on a format specification.
• The format specification string, along with the data to be output, are the parameters to
the printf() function

• Syntax:
Other Standard Functions printf (format, data1, data2,……..);
In this syntax format is the format specification string. This string contains, for each
variable to be output, a specification beginning with the symbol % followed by a character
called the conversion character.

Srinivas V, +91 99499 10792 34 Srinivas V, +91 99499 10792 37


Thursday, December 5, 2024

Character Input Functions


• Character Input functions
• Character input functions is used to input a single character. All these functions are available in
'stdio.h' header file.
• getch(): Use to input single character at a time. But it will not display input character. get stands
for input, ch stands for character.
Syntax: char a = getch();

• getche(): It is same as getch() function but it will display input character. get stands for input, ch
stands for character, e stands for echo(display)
Syntax: char a = getche();
• Both getch() and getche() functions will not required enter key to terminate.

• getchar(): It will terminate by enter key after inputting a single character.


Syntax: char a = getchar();

Srinivas V, +91 99499 10792 38

Character Output Functions

• Character output functions

• 1. putch(): use to print a single character.


• Syntax: putch(a);

• 2. putchar(): use to print a single character.


• Syntax: putchar(a);

Srinivas V, +91 99499 10792 39

Binary Input Output Function

• A binary file is a file that uses all 8 bits of a byte for storing the information . The only
difference between the text file and binary file is the data contain in text file can be
recognized by the word processor while binary file data can't be recognized by a word
processor. ...

• In binary files, each byte may not be a character

NOTE : Formatted input/output(printf,scanf) ,


• character input/output(getchar, putchar),
• string input/output(fgets, fputs) functions can be used only with text files.

Srinivas V, +91 99499 10792 40

You might also like