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

Functions

Uploaded by

farhanshafaut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Functions

Uploaded by

farhanshafaut
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

FUNCTION

ADIBA IBNAT HOSSAIN


LECTURER, DEPARTMENT OF CSE, PREMIER UNIVERSITY
BASICS OF FUNCTION

❖ Function is a block of code that performs a specific task.


❖ There are two types of function in C programming:
• Standard library functions
• User-defined functions

❖ Why do we use function?


• To break down or decompose a problem into smaller chunks.
• It avoids repetition and makes a high degree of code reusing.
BASICS OF FUNCTION(CONTD.)

❖ Declaration of a function:
A function declaration tells the compiler about a function's name, return type,
and parameters.

❖ Definition of a function:
A function definition provides the actual body of the function.

❖ Calling a function:
To use a function, you will have to call that function to perform the defined
task.
FUNCTION DECLARATION

❖ A function declaration has the following parts −


return_type function_name( parameter list );
❖ For the function max(), the function declaration is as follows −
int max(int num1, int num2);
❖ Parameter names are not important in function declaration only their
type is required, so the following is also a valid declaration −
int max(int, int);
CALLING A FUNCTION

❖ 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.

❖ When a program calls a function, the program control is transferred to the called
function.

❖ A called function performs a defined task.

❖ 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.
FUNCTION DEFINITION

❖ The general form of a function definition in C programming language is as follows −


return_type function_name( parameter list )
{ body of the function }

❖ Given below is the source code for a function called max(). This function takes two parameters
num1 and num2 and returns the maximum value between the two −

int max(int num1, int num2)


{
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
ARGUMENT AND PARAMETER

❖ Arguments are the actual values of the


variables that get passed to the function.
They are called actual parameters.

❖ Parameters are the names listed in the


function's definition. They are called formal
parameters.
CALCULATE CUBE OF A NUMBER
❖ Write a C program to calculate cube of a number.
CHECK EVEN OR ODD
❖ Write a C program to check whether a number is even or odd.
CHECK PRIME NUMBER OR NOT
❖ Write a C program to check whether a given number is even or odd.
RECURSION

❖ A function that calls itself is known as a recursive function. And, this technique is known as
recursion.

❖ How recursion works?


void func()
{
... .. ...
func();
... .. ...
}
int main()
{
... .. ...
func();
... .. ...
}
}
COMPUTING FACTORIAL USING RECURSION

❖ Write a C program for computing Factorial of a number using recursion.


COMPUTING FIBONACCI SERIES USING RECURSION
❖ Write a C program for computing Fibonacci series using recursion.

You might also like