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

Class 3 Notes

The document discusses functions in C programming. It defines a function as a block of code that performs a specific task and can take inputs and return outputs. It provides examples of function declarations, definitions, and calls. It discusses different ways of passing parameters to functions by value and by reference. It also covers different types of functions, recursion, and some key points about functions in C.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Class 3 Notes

The document discusses functions in C programming. It defines a function as a block of code that performs a specific task and can take inputs and return outputs. It provides examples of function declarations, definitions, and calls. It discusses different ways of passing parameters to functions by value and by reference. It also covers different types of functions, recursion, and some key points about functions in C.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Functions

A function is a set of statements that take inputs, do some specific computation and
produces output.
#include <stdio.h>

// An example function that takes two parameters 'x' and 'y'


// as input and returns max of two input numbers
int max(int x, int y)
{
if (x > y)
return x;
else
return y;
}

// main function that doesn't receive any parameter and


// returns integer.
int main(void)
{
int a = 10, b = 20;

// Calling above function to find max of 'a' and 'b'


int m = max(a, b);

printf("m is %d", m);


return 0;
}

Output:
m is 20
Why do we need functions?

• Functions help us in reducing code redundancy.

• Functions make code modular.

• Functions provide abstraction. For example, we can use


library functions without worrying about their internal
working.
Function Declaration

A function declaration tells the compiler about


1. The number of parameters function takes
2. data-types of parameters and
3. Return type of function.

Putting parameter names in function declaration is


optional in the function declaration, but it is necessary to
put them in the definition.
// A function that takes two integers as
parameters
// and returns an integer
int max(int, int);

// A function that takes a int pointer and an


int variable as parameters
// and returns an pointer of type int
int *swap(int*,int);

// A function that takes a char as parameters


// and returns an reference variable
char *call(char b);

// A function that takes a char and an int as


parameters
// and returns an integer
int fun(char, int);
Parameter Passing to functions

The parameters passed to function are called actual parameters.


The parameters received by function are called formal parameters.

There are two most popular ways to pass parameters.

Pass by Value: In this parameter passing method, values of actual parameters are copied to function’s formal
parameters

The two types of parameters are stored in different memory locations.

So any changes made inside functions are not reflected in actual parameters of caller.

Pass by Reference Both actual and formal parameters refer to same locations,

so any changes made inside the function are actually reflected in actual parameters of caller.
#include <stdio.h>
void fun(int x)
{
x = 30;
}

int main(void)
{
int x = 20;
fun(x);
printf("x = %d", x);
return 0;
}
# include <stdio.h>
void fun(int *ptr)
{
*ptr = 30;
}

int main()
{
int x = 20;
fun(&x);
printf("x = %d", x);

return 0;
}
Following are some important points about functions in C.

1) Every C program has a function called main() that is called by


operating system when a user runs the program.

2) Every function has a return type. If a function doesn’t return any value,
then void is used as return type.

3) In C, functions can return any type except arrays and functions.

4) Empty parameter list in C mean that the parameter list is not specified

5) In C program, a function is called before its declaration then the C


compiler automatically assumes the declaration of that function in the
following way:
int function name();
Types of function
There are two types of function in C programming:

• Standard library functions

• User-defined functions
There can be 4 different types of user-defined functions,
they are:
1. Function with no arguments and no return value
Void sum();//function declaration
2. Function with no arguments and a return value
int sum();//function declaration
3. Function with arguments and no return value
Void sum(int,int);//function declaration
4. Function with arguments and a return value
int sum(int,int)
Recursion

The process in which a function calls itself directly


or indirectly is called recursion and the
corresponding function is called as recursive
function.
// C code to implement factorial
#include <stdio.h>

// Factorial function
int f(int n)
{
// Stop condition
if (n == 0 || n == 1)
return 1;

// Recursive condition
else
return n * f(n - 1);
}
int main()
{
int n = 5;
printf("factorial of %d is: %d",
n, f(n));
return 0;
}

You might also like