0% found this document useful (0 votes)
17 views

C Programming - Lecture 5

Uploaded by

Daring Hunterz
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)
17 views

C Programming - Lecture 5

Uploaded by

Daring Hunterz
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/ 38

Functions in C

Courtesy: Dr. Md. Alamgir Hosssain, Associate Professor, Dept. of ICT, IU

1/38
Index
§ Function § Types of user-defined functions
§ No Argument Passed and No
§ Types of functions Return value
§ User-defined § No Argument Passed But Return
function Value
§ Function § Argument Passed But No Return
prototypes Value
§ Calling Function § Argument Passed and Returns a
§ Function Definition Value
§ Passing arguments § Recursion
to a function § Storage class
§ Return Statement § automatic
§ external
§ static
§ register

2/38
What is Function? (1/2)

§ Function is a self contained block of statements that


perform a coherent task of some kind

§ Every C program can be a thought of the collection of


functions

§ main( ) is also a function

3/38
What is Function? (2/2)

§ Suppose, you need to create a program to create a


circle and color it.

You can create two functions to solve this problem:


§ create a circle function
§ create a color function

§ Dividing a complex problem into smaller chunks makes


our program easy to understand and reuse.

4/38
Types of Function

Library functions
These are the built in functions of of ‘‘C’’ library. These are
already defined in header files
e.g. printf ( ); is a function which is used to print at output.
It is defined in‘‘stdio.h’’

User defined functions


Programmer can create their own function in C to perform
specific task

5/38
How user-defined function works

6/38
Advantages of user-defined function

§ The program will be easier to understand, maintain and


debug

§ Reusable codes that can be used in other programs

§ A large project can be divided among many


programmers

7/38
Example: C user-defined functions

8/38
Function prototype (1/2)

§ A function prototype is simply the declaration of a


function that specifies function's name, parameters and
return type. It doesn't contain function body

§ A function prototype gives information to the compiler


that the function may later be used in the program

Syntax of Function prototype

9/38
Function prototype (2/2)

In the above example, int addNumbers(int a, int b); is


the function prototype which provides the following
information to the compiler:

§ name of the function is addNumbers()


§ return type of the function is int
§ two arguments of type int are passed to the function

The function prototype is not needed if the user-defined


function is defined before the main() function

10/38
Calling Function

§ Control of the program is transferred to the user-


defined function by calling it

Syntax of function call

In the above example, the function call is made using


addNumbers(n1, n2);
statement inside the main() function

11/38
Function Definition

§ Function definition contains the block of code to perform


a specific task
§ In our example, adding two numbers and returning it
Syntax of function definition

§ When a function is called, the control of the program is


transferred to the function definition
§ And, the compiler starts executing the codes inside the
body of a function

12/38
Passing arguments to a function

13/38
Return Statement

14/38
Syntax of return statement

Return statement syntax:

For Example:

15/38
Types of user-defined functions

§ No Argument Passed and No Return value


§ No Argument Passed But Return Value
§ Argument Passed But No Return Value
§ Argument Passed and Return a Value

16/38
No Argument Passed and No Return
Value

17/38
No Argument Passed and but Return
Value

18/38
Argument Passed and But No Return
Value

19/38
Argument Passed and Return Value

20/38
C Recursion

§ A function that calls itself is known as a


recursive function
§ And, this technique is known as recursion

21/38
How Recursion Works

22/38
Recursion Example

23/38
Explanation:

24/38
Storage Class

§ Storage class determines the scope, visibility and


lifetime of a variable.

There are 4 types of storage class:


§ automatic
§ external
§ static
§ register

25/38
Storage Class: Automatic/Local

§ The variables declared inside a block are automatic


or local variables

§ The local variables exist only inside the block in


which it is declared

26/38
Storage Class: Automatic (Example)

27/38
Storage Class: External/Global

§ Variables that are declared outside of all functions


are known as external or global variables

§ They are accessible from any function inside the


program

28/38
Storage Class: External (Example)

29/38
Storage Class: Register

§ The “register” keyword is used to declare register


variables
§ Scope of a register variable is local to the block in
which it is defined
§ Lifetime is till control remains within the block in
which the register variable is defined
§ Register variables tell the compiler to store the
variable in CPU register instead of memory
§ Frequently used variables are kept in registers and
they have faster accessibility
§ We can never get the addresses of these variables.
§ Register can not be used with static
30/38
Storage Class: Accessing Address of
Register
§ If you use & operator with a register variable then
compiler may give an error or warning
§ (depending upon the compiler you are using),
§ because when we say a variable is a register,
§ it may be stored in a register instead of memory
and accessing address of a register is invalid

31/38
Storage Class: Register with pointer
variables
§ A register can have address of a memory location.

32/38
Storage Class: Register not with
static
§ C doesn’t allow multiple storage class specifiers for
a variable.
§ So, register can not be used with static

33/38
Storage Class: Register only within
a block

§ It can not be used in the global scope (outside


main)

34/38
Storage Class: Static

§ A static variable is declared by using the static


keyword

For example:
static int i;

§ The value of a static variable persists until the end


of the program

35/38
Storage Class: static (Example)

Output:

6 11

36/38
Thank You

37/38
References

https://www.programiz.com/c-programming/c-storage-class

https://www.geeksforgeeks.org/understanding-register-keyword/

https://www.tutorialspoint.com/register-keyword-in-c

38/38

You might also like