0% found this document useful (0 votes)
345 views13 pages

FUNCTIONS in C

The document discusses functions in C programming. It defines a function as a block of code that performs a specific task. Functions allow programmers to divide large programs into smaller, reusable components. There are four main aspects of functions discussed: functions without arguments/return value, functions without arguments but with return value, functions with arguments but without return value, and functions with both arguments and return value. The document also covers function declaration, definition, calling, prototypes, types of functions, and call by value vs. call by reference.

Uploaded by

Citu
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)
345 views13 pages

FUNCTIONS in C

The document discusses functions in C programming. It defines a function as a block of code that performs a specific task. Functions allow programmers to divide large programs into smaller, reusable components. There are four main aspects of functions discussed: functions without arguments/return value, functions without arguments but with return value, functions with arguments but without return value, and functions with both arguments and return value. The document also covers function declaration, definition, calling, prototypes, types of functions, and call by value vs. call by reference.

Uploaded by

Citu
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

“FUNCTIONS in C”

Reetu Bhardwaj

Date: 10/11/2020

A function is a block of code that performs a specific task.


In c, we can divide a large program into the basic building blocks known as
function. The function contains the set of programming statements enclosed by {}.
A function can be called multiple times to provide reusability and modularity to the
C program. In other words, we can say that the collection of functions creates a
program

Advantage of functions in C
There are the following advantages of C functions.

o By using functions, we can avoid rewriting same logic/code again and again
in a program.
o We can call C functions any number of times in a program and from any
place in a program.
o We can track a large C program easily when it is divided into multiple
functions.
o Reusability is the main achievement of C functions.
o However, Function calling is always a overhead in a C program

Function Aspects
Function declaration

A function must be declared globally in a c program to tell the compiler


about the function name, function parameters, and return type.

 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/Declaration

returnType functionName(type1 argument1, type2 argument2, ...);

e. g int sum(int a, int b);

or int sum(int,int); // two arguments of integer type will be used

here, returnType is integer, function name is sum and two arguments of type int
are passed to the function

note : The function prototype is not needed if the user-defined function is


defined before the main() function.

Function call

Control of the program is transferred to the user-defined function by calling it.


Syntax of function call

functionName(argument1, argument2, ...);

example, the function call is made using sum(n1, n2); statement inside the main()
function.

 Function can be called from anywhere in the program.


 The parameter list must not differ in function calling and function
declaration.
 We must pass the same number of functions as it is declared in the function
declaration.

Function definition

It contains the actual statements which are to be executed. It is the most


important aspect to which the control comes when the function is called.
Here, we must notice that only one value can be returned from the function.
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

returnType functionName(type1 argument1, type2 argument2, ...)

//body of the function

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.

Types of Functions

There are two types of functions in C programming:

Library Functions: are the functions which are declared in the C header
files such as scanf(), printf(), gets(), puts(), sqrt(),clrscr() etc.

User-defined functions: are the functions which are created by the C


programmer, so that he/she can use it many times. It reduces the complexity
of a big program and optimizes the code.

Return Value

The return statement terminates the execution of a function and returns a


value to the calling function. The program control is transferred to the
calling function after the return statement.

A C function may or may not return a value from the function. If you don't
have to return any value from the function, use void for the return type.

Let's see a simple example of C function that doesn't return any value from
the function.
Example without return value:

void hello()

printf("hello c");

Example with return value:


int get(){
return 10;
}

TYPES OF FUNCTIONS (ASPECTS OF FUNCTION)


A function may or may not accept any argument. It may or may not return any
value. Based on these facts, there are four different aspects of function calls.

 function without arguments and without return value(Takes nothing, Return


nothing
 function without arguments and with return value(Takes Nothing, Return
Something)
 function with arguments and without return value(Takes something, Return
nothing)
 function with arguments and with return value(Takes something, Return
something)
1. Function without arguments and without return value

#include<stdio.h>

void printName();

void main ()

printf("Hello ");

printName();

void printName()

printf("Hello World");

Example 2:
#include<stdio.h>

void sum();

void main()

printf("\n calculate the sum of two numbers:");

sum();

void sum()

int a,b;

printf("\nEnter two numbers");

scanf("%d %d",&a,&b);

printf("The sum is %d",a+b);

}
Example 3:
#include <stdio.h>
void checkPrimeNumber();
int main()
{
checkPrimeNumber(); // argument is not passed
return 0;
}

// return type is void meaning doesn't return any value


void checkPrimeNumber()
{
int n, i, flag = 0;

printf("Enter a positive integer: ");


scanf("%d",&n);

for(i=2; i <= n/2; ++i)


{
if(n%i == 0)
{
flag = 1;
}
}
if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);
}

2. Function without argument and with return value

Example 1:

#include<stdio.h>
int sum();
void main()
{
int result;
printf("\n calculate the sum of two numbers:");
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}

Example 2:

#include <stdio.h>
int getInteger();

int main()
{
int n, i, flag = 0;

// no argument is passed
n = getInteger();

for(i=2; i<=n/2; ++i)


{
if(n%i==0){
flag = 1;
break;
}
}

if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);

return 0;
}

// returns integer entered by the user


int getInteger()
{
int n;

printf("Enter a positive integer: ");


scanf("%d",&n);

return n;
}
3. Function with argument and without return value

Example 1:

#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result;
printf("\ calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b); //Actual parameters
}
void sum(int a, int b) // Formal parameter
{
printf("\nThe sum is %d",a+b);
}

Example 2:

program to calculate the average of five numbers.

#include<stdio.h>
void average(int, int, int, int, int);
void main()
{
int a,b,c,d,e;
printf("\nGoing to calculate the average of five numbers:");
printf("\nEnter five numbers:");
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
average(a,b,c,d,e);
}
void average(int a, int b, int c, int d, int e)
{
float avg;
avg = (a+b+c+d+e)/5;
printf("The average of given five numbers : %f",avg);
}
4. Function with argument and with return value

Example 1:
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nGoing to calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
}
int sum(int a, int b)
{
return a+b;
}

Example 2:
#include<stdio.h>
int even_odd(int);
void main()
{
int n,flag=0;
printf("\n check whether a number is even or odd");
printf("\nEnter the number: ");
scanf("%d",&n);
flag = even_odd(n);
if(flag == 0)
{
printf("\nThe number is odd");
}
else
{
printf("\nThe number is even");
}
}
int even_odd(int n)
{
if(n%2 == 0)
{
return 1;
}
else
{
return 0;
}
}
*Which approach is better?*

Well, it depends on the problem you are trying to solve. In this case, passing argument
and returning a value from the function (example 4) is better.

Call by value and Call by reference in C

There are two methods to pass the data into the function in C language, i.e., call by value
and call by reference.

Call by value in C
 In call by value method, the value of the actual parameters is copied into the
formal parameters. In other words, we can say that the value of the variable is
used in the function call in the call by value method.
 In call by value method, we can not modify the value of the actual parameter by
the formal parameter.
 In call by value, different memory is allocated for actual and formal parameters
since the value of the actual parameter is copied into the formal parameter.

 The actual parameter is the argument which is used in the function call whereas
formal parameter is the argument which is used in the function definition.
Example: 1

#include<stdio.h>
void change(int num)
{
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x);//passing value in function
printf("After function call x=%d \n", x);
return 0;
}

Example 2: Swapping the values of the two variables

#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
// printing the value of a and b in main
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
// The value of actual parameters do not change by changing the formal parameters in call
by value, a = 10, b = 20
}
void swap (int a, int b)
{
int temp;
temp = a; // temp = 10
a=b; // a= 20
b=temp; // b= 10
printf("After swapping values in function a = %d, b = %d\n",a,b);
// Formal parameters, a = 20, b = 10
}
Call by reference in C

In call by reference, the address of the variable is passed into the function call as the
actual parameter.
 The value of the actual parameters can be modified by changing the formal
parameters since the address of the actual parameters is passed.
 In call by reference, the memory allocation is similar for both formal parameters
and actual parameters.
 All the operations in the function are performed on the value stored at the address
of the actual parameters, and the modified value gets stored at the same address.

Int x= 67;

X= name of the memory block(variable)


67 Content in memory block;

2048 Address of memory block

Example 1:

#include <stdio.h>
void increment(int *var)
{

*var = *var+1;
}
int main()
{
int num=20;

increment (&num);
printf("Value of num is: %d", num);
return 0;
}
Example: 2.

#include<stdio.h>
void change(int *num) {
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
return 0;
}

Example 3.

#include <stdio.h>
void swapnum ( int *var1, int *var2 )
{
int tempnum ;
tempnum = *var1 ;
*var1 = *var2 ;
*var2 = tempnum ;
}
int main( )
{
int num1 = 35, num2 = 45 ;
printf("Before swapping:");
printf("\nnum1 value is %d", num1);
printf("\nnum2 value is %d", num2);

/*calling swap function*/


swapnum( &num1, &num2 );

printf("\nAfter swapping:");
printf("\nnum1 value is %d", num1);
printf("\nnum2 value is %d", num2);
return 0;
}

You might also like