Module 3 - Syllabus
• Functions - Function definition, Function call, Function prototype, Parameter
passing; Recursion; Passing array to function;
• Macros - Defining and calling macros;
• Command line Arguments.
• Structures - Defining a Structure variable, Accessing members, Array of
structures, Passing structure to function; Union.
• Storage Class - Storage Classes associated with variables: automatic, static,
external and register.
C Functions
• A function in C is a set of statements that when called perform some specific
tasks. A function is a block of code which only runs when it is called.
• It is the basic building block of a C program that provides modularity and code
reusability.
• Functions are used to perform certain actions, and they are important for
reusing code: Define the code once, and use it many times.
• You can pass data, known as parameters, into a function.
Consider a simple C program
#include <stdio.h>
int main()
{
int add;
add=10+30;
printf("Sum is: %d", add);
return 0;
}
Same program using functions
#include<stdio.h>
//function declaration
int sum(int a, int b);
int main()
{
// Calling sum function and
S2-GXEST 204-PROGRAMMING IN C (2024 Scheme) Reenu Renjith - Assistant Professor CSE - MBITS 1
, // storing its value in add variable
int add = sum(10, 30);
printf("Sum is: %d", add);
return 0;
}
//function definition
int sum(int a, int b)
{
return a + b;
}
Function Declarations/Function Prototype
• In a function declaration, we must provide the function name, its return type, and
the number and type of its parameters.
• A function declaration tells the compiler that there is a function with the given
name defined somewhere else in the program.
Syntax
return_type name_of_the_function (parameter_1, parameter_2);
The parameter name is not mandatory while declaring functions. We can also
declare the function without using the name of the data variables.
Example
int sum(int a, int b); // Function declaration with parameter names
int sum(int , int); // Function declaration without parameter names
#include<stdio.h>
//function declaration
int sum(int a, int b);
int main()
{
// Calling sum function and
// storing its value in add variable
int add = sum(10, 30);
printf("Sum is: %d", add);
return 0;
}
//function definition
int sum(int a, int b)
S2-GXEST 204-PROGRAMMING IN C (2024 Scheme) Reenu Renjith - Assistant Professor CSE - MBITS 2
,{
return a + b;
}
Function Definition
• The function definition consists of actual statements which are executed when
the function is called (i.e. when the program control comes to the function).
• Syntax:
return_type function_name (para1_type para1_name, para2_type para2_name)
{
// body of the function
}
• Either function can be declared first and then defined later in the program
• Or a function can be defined and declared in a single step also (but when we do
so, make sure the function definition is written before main program).
• The below example serves as both a function definition and a declaration.
#include<stdio.h>
//function definition and declaration together before main program
int sum(int a, int b)
{
return a + b;
}
int main()
{
// Calling sum function and
// storing its value in add variable
int add = sum(10, 30);
printf("Sum is: %d", add);
return 0;
}
S2-GXEST 204-PROGRAMMING IN C (2024 Scheme) Reenu Renjith - Assistant Professor CSE - MBITS 3
, Function Call
• A function call is a statement that instructs the compiler to execute the function.
• We use the function name and parameters in the function call.
• In the below example, the first sum function is called and 10,30 are passed to the
sum function.
• After the function call sum of a and b is returned and control is also returned
back to the main function of the program.
Conditions of Return Types and Arguments
• In C programming language, functions can be called either with or without
arguments and might return values.
• They may or might not return values to the calling functions.
1. Function with no arguments and no return value
2. Function with no arguments and with return value
3. Function with argument and with no return value
4. Function with arguments and with return value
Types of Functions
There are two types of functions in C:
1. Library Functions
2. User Defined Functions
1. Library Function
• A library function is also referred to as a “built-in function”.
• A compiler package already exists that contains these functions.
S2-GXEST 204-PROGRAMMING IN C (2024 Scheme) Reenu Renjith - Assistant Professor CSE - MBITS 4