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

Functions (1)

A simple presentation on functions in C

Uploaded by

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

Functions (1)

A simple presentation on functions in C

Uploaded by

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

Functions in C

Types of function

There are two types of function in C programming:


• Standard library functions
• User-defined functions
USER DEFINED
FUNCTIONS
Definition - Function

• functions are used to compute a value or perform a specific


task.
• They can’t run independently and are always called by the
main() program or by some other function.
Structure of C Program
User Defined Functions

• used to reduce the difficulties during debugging a program.


• User defined functions are self-contained blocks of statements
which are written by the user to compute or perform a task.
• called by the main program repeatedly as per the requirement.
• useful when program size are too large and complex.
• works like top-down modular programming technique to solve a
problem.
Uses of functions

• The length of a source program can be reduced by using functions


• It is easy to locate and isolate a faulty function for further
investigations.
• A function can be used by many other programs.
Thus C programmer can build their own library.
Top-down modular
programming using functions
Modular Programming

• It is defined as organizing a larger program in to small,


independent program segments called modules.

• It is basically divide and conquer approach to problem solving


Elements of User defined Functions

Three elements of functions are:


1. Function declaration
2. Function Call
3. Function definition
Function - Definition

• The function definition is an independent program module that is


specially written or implement the requirements of the function.
• To use this block or function, we need to invoke it at the required
place in the program, known as the functions
• The program that calls the function is referred to as the calling
program or calling functions
Function - Definition
FUNCTION
DEFINITION

A function definition includes the following elements


• Function name
• Function type
• List of parameters
• Local variable declarations
• A return statement
Syntax of function definition
• If no return data type is specified then by default ‘C’ will assume
that it is an integer type.
• If the function is not going to return any value then we have to
specify the return type as void.
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
return 0;
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
Function Prototype
• A function prototype is 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:
returnType functionName(type1 argument1, type 2 argument2,...);
Calling a function
• Control of the program is transferred to the user-defined function
by calling it.
Syntax :
functionName(argument1, argument2, ...);

Note: Function call should be performed within the main() function.


Function definition

• Function definition contains the block of code to perform a specific


task.
Syntax
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.
Passing arguments to a function
• In programming, argument refers to the variable passed to the
function.
• two variables n1 and n2 are passed during the function call.
• The parameters a and b accepts the passed arguments in the
function definition.
• These arguments are called formal parameters of the function.
Formal and Actual Arguments

Actual arguments Formal Arguments


• The argument listed in the function • The argument used in the function
calling statement are referred to as declaration are referred as formal
actual arguments. arguments.
• They are the actual values passed to a • They are formal variables that accepts or
function to compute a value or to receive the values supplied by the calling
perform a task. program.
Rules of call a
function

• Function is called by the main() or any other function


• When the return type of the function is omitted, then by default
the return type will be int.
• A function can return a value any stage of its execution by using
more than one return statements.
• The return statement is omitted if it does not return a value
Arguments listed can be omitted, but not the parenthesis ()
Return values and their types

• We can pass n numbers of values to the called function, but the


called function can only return one value per call.

The return statement can take one of the following form


• return;
• return(expression);
• The return does not return any value
• return statement with expression returns the value of the
expression
• There can be more than one return statement if there is use of
conditional statement.
Category of functions
Functions are categorized based on the arguments.
The category of functions are:

• Functions with no arguments and no return values.


• Functions with arguments and no return values.
• Functions with arguments and one return value.
• Functions with no arguments but return a values.
Functions with no arguments and no return values

• You do not pass data to the called function.


• function with no return type does not pass back data to the
calling function.
• This type of function which does not return any value cannot be
used in an expression
• it can be used only as independent statement.
Functions with no arguments and no return values
Functions with no arguments and no
return values
#include<stdio.h>
void sum();
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);
}
Functions with arguments and no return value

• It accept data/parameters from calling function.


• It sends parameters/data to the called function from
calling function but you cannot send result data back
to the calling function.
Functions with arguments and no return value
Functions with arguments and no return value

#include<stdio.h>
void sum(int, int);
main()
{
int a,b,result;
printf("Calculate the sum of two numbers:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
void sum(int a, int b)
{
printf("\nThe sum is %d",a+b);
}
Functions with arguments and return
value

• It can send arguments from the calling function to the called


function
• It wait for the result to be returned back from the called function
back to the calling function
• This type of function is mostly used in programming world
because it can do two way communications
Functions with arguments and return
value

• It can accept data as arguments as well as can send back data as


return value
• The data returned by the function can be used later in our
program for further calculations.
Functions with arguments and return
value
Functions with arguments and return value

#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nCalculate 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;
}
Functions with no arguments and return value

• A function which does not take any argument but only returns
values to the calling function.
• The best example of this type of function is “getchar()” library
function which is declared in the header file “stdio.h”.
Functions with no arguments but returns value
Functions with no arguments but returns value

#include<stdio.h>
int sum();
main()
{
int result;
printf("\n Calculate the sum of two numbers:");
result = sum();
printf(" The sum of two numbers is = %d\
n",result);
}
int sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Recursion
• A function calling itself repetitively to compute a value
• Functions are called by main function or by some other function
• But in recursion the same function is called by itself repeatedly
Use of Recursive Function

• Recursive functions are written with less number of statements


• Recursion is effective where terms are generated successively to
compute a value
#include <stdio.h>
int fact (int);
main() int fact(int n)
{ {
int n,f; if (n==0)
printf("Enter the number \n"); {
scanf("%d",&n); return 0;
f = fact(n); }
printf("factorial = %d",f); else if ( n == 1)
} {
return 1;
}
else
{
return n*fact(n-
1);
}
}

You might also like