Chapter 5 Functions
Chapter 5 Functions
FUNCTIONS
Prepared By:
Subject : PPS
Asst. Prof. Rupali Patel
Code : 3110003 (CSE Department, ACET)
Functions
A function is a group of statements that together perform a task. Every C
program has at least one function, which is main().
A function declaration tells the compiler about a function's name, return
type, and parameters. A function definition provides the actual body of
the function.
Defining a Function
The general form of a function definition in C programming language is
as follows −
return_type function_name( parameter list )
{
body of the function
}
Functions (cont..)
Return Type − A function may return a value. The return_type is the
data type of the value the function returns. Some functions perform the
desired operations without returning a value. In this case, the return_type
is the keyword void.
Function Name − This is the actual name of the function.
Parameters − A parameter is like a placeholder. When a function is
invoked, you pass a value to the parameter. This value is referred to as
actual parameter or argument. The parameter list refers to the type, order,
and number of the parameters of a function. Parameters are optional; that
is, a function may contain no parameters.
Function Body − The function body contains a collection of statements
that define what the function does.
Function Declarations
A function declaration tells the compiler about a function name and
how to call the function. The actual body of the function can be defined
separately.
A called function performs a defined task and when its return statement is
executed or when its function-ending closing brace is reached, it returns
the program control back to the main program.
To call a function, you simply need to pass the required parameters along
with the function name, and if the function returns a value, then you can
store the returned value.
Parameter Passing In C
In C, there are two types of parameters and they are as follows
• Actual Parameters
• Formal Parameters
The actual parameters are the parameters that are speficified in calling
function.
The formal parameters are the parameters that are declared at called
function. When a function gets executed, the copy of actual parameter
values are copied into formal parameters.
Call by Value
In call by value parameter passing method, the copy of actual parameter
values are copied to formal parameters and these formal parameters are
used in called function.
The changes made on the formal parameters does not effect the
values of actual parameters.
That means, after the execution control comes back to the calling
function, the actual parameter values remains same. For example
consider the following program
Call by Value Example
#include<stdio.h> getch() ;
#include<conio.h> }
void swap(int,int) ; // function
declaration void swap(int a, int b) // called
void main() function
{ {
int num1, num2 ; int temp ;
num1 = 10 ; temp = a ; temp=10;
num2 = 20 ; a=b; a=20;
printf("\nBefore swap: num1 = b = temp ; b=10;
%d, num2 = %d", num1, num2) ; }
swap(num1, num2) ;
printf("\nAfter swap: num1 =
%d\nnum2 = %d", num1, num2);
Call By Reference
In Call by Reference parameter passing method, the memory location
address of the actual parameters is copied to formal parameters.
This address is used to access the memory locations of the actual
parameters in called function.
In this method of parameter passing, the formal parameters must
be pointer variables.That means in call by reference parameter passing
method, the address of the actual parameters is passed to the called
function and is recieved by the formal parameters (pointers).
Whenever we use these formal parameters in called function, they
directly access the memory locations of actual parameters.
the changes made on the formal parameters effects the values of
actual parameters.
Call By Reference Example
#include<stdio.h> printf("\nAfter swap: num1 =
#include<conio.h> %d, num2 = %d", num1, num2);
void swap(int *,int *) ; // getch() ;
function declaration }
void main() void swap(int *a, int *b) //
{ called function
int num1, num2 ; {
int temp ;
num1 = 10 ; temp = *a ;
num2 = 20 ; *a = *b ;
printf("\nBefore swap: num1 = *b = temp ;
%d, num2 = %d", num1, num2) ; }
swap(&num1, &num2) ; //
calling function
C Macros
A macro is a fragment of code which has been given a name.
There are two kinds of macros. They differ mostly in what they look like
when they are used.
#include <stdio.h>
#include "myheader.h"
These directives tell the CPP to get stdio.h from System Libraries and
add the text to the current source file. The next line tells CPP to
get myheader.h from the local directory and add the content to the
current source file.