SlideShare a Scribd company logo
C
Programming
Functions
By:
Er.Anupam Sharma
Introduction
A function is a block of code performing a specific task
which canbe calledfrom other parts of a program.
The name of the function is unique in a C Program and
is Global. It means that a function can be accessed
from any location with in aCProgram.
We pass information to the function called arguments
specified when the function is called. And the function
either returns some value to the point it was called
from or returnsnothing.
Program to find if the number is Armstrong or not
#include<stdio.h>
#include<conio.h>
int cube(int);
void main()
{
clrscr();
int num, org, remainder,sum=0;
printf("Enter any numbern");
scanf("%d",&num);
org=num;
while(num!=0)
{
remainder=num%10;
sum=sum+cube(remainder);
num=num/10;
}
if(org==sum)
{
printf("nThe number is armstrong");
}
else
{
printf("nThe number is not armstrong");
}
getch();
}
int cube(int n)
{
int qbe;
qbe=n*n*n;
return qbe;
}
Second way:
#include<stdio.h>
#include<conio.h>
int sum_cube_digits(int);
void main()
{
clrscr();
int num, org, f_call;
printf("Enter any numbern");
scanf("%d",&num);
org=num;
f_call=sum_cube_digits(num);
if(org==f_call)
{
printf("nThe number is armstrong");
}
else
{
printf("nThe number is not armstrong");
}
getch();
}
int sum_cube_digits(int n)
{
int rem ,sum=0;
while(n!=0)
{
remainder=n%10;
sum=sum+rem*rem*rem;
n=n/10;
}
return sum;
}
Third way:
#include<stdio.h>
#include<conio.h>
int armstrong(int);
void main()
{
clrscr();
int num;
printf("Enter any numbern");
scanf("%d",&num);
armstrong(num);
getch();
}
int armstrong(int n)
{
int remainder,sum=0,org;
org=n;
while(n!=0)
{
remainder=n%10;
sum=sum+remainder*remainder*remaind
er;
n=n/10;
}
if(org==sum)
{
printf("nThe number is armstrong");
}
else
{
printf("nThe number is not armstrong");
}
return(0);
}
Program to calculate factorial of a number.
#include <stdio.h>
#include <conio.h>
int calc_factorial (int); // ANSI function prototype
void main()
{
clrscr();
int number;
printf("Enter a numbern");
scanf("%d", &number);
calc_factorial (number);// argument ‘number’ is passed
getch();
}
int calc_factorial (int i)
{
int loop, factorial_number = 1;
for (loop=1; loop <=i; loop++)
factorial_number *= loop;
printf("The factorial of %d is %dn",i, factorial_number);
}
Function Prototype
int calc_factorial (int);
 The prototype of a function provides the basic information about a
function which tells the compiler that the function is used correctly
or not. It contains the same information as the function header
contains.
 The only difference between the header and the prototype is the
semicolon;there must the a semicolonat the end of the prototype.
Defining a Function:
 Thegeneralform of a functiondefinition is as follows:
return_type function_name( parameter list )
{
body of the function
}
 Return Type: 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 keywordvoid.
 Function Name: This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
 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.
Parameter names are not important in function declaration only their type is
required
 Function Body: The function body contains a collection of statements that define
what the functiondoes.
More about Function
Function declaration is required when you define a function in one source file
and you call that function in another file. In such case you should declare the
functionat the top of thefile callingthe function.
Calling aFunction:
While creating a Cfunction, you give a definition of what the function has to
do.Touse a function,you will haveto callthat functionto perform the defined
task.
When a program calls a function,program control is transferred to the called
function.A called function performs defined task and when its return
statementis executedor when its function-endingclosing brace is reached,it
returns program control back to the mainprogram.
Tocalla function,you simply need to pass the requiredparameters along with
function name, and if function returns a value,then you can store returned
value.
For Example
#include <stdio.h>
#include<conio.h>
int max(int num1,int num2); /* function declaration*/
int main ()
{
int a =100;
/* local variable definition */int b =200;
int ret;
ret =max(a,b); /* callinga function to getmaxvalue*/
printf( "Maxvalue is :%dn", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1,intnum2)
{
int result; /*local variable declaration */
if (num1 >num2)
result =num1;
else
result =num2;
return result;
}
Local and global variables
 Local:
These variables only exist inside the specific function that creates them.
They are unknown to other functions and to the main program. As such,
they are normally implemented using a stack. Local variables cease to
exist once the function that created them is completed. They are
recreatedeach time a function is executedorcalled.
 Global:
These variables can be accessed by any function comprising the program.
They do not get recreated if the function is recalled. To declare a global
variable, declare it outside of all the functions. There is no general rule for
where outside the functions these should be declared, but declaring them
on top of the code is normally recommended for reasons of scope. If a
variable of the same name is declared both within a function and outside
of it, the function will use the variable that was declared within it and
ignore the globalone.
Function Arguments:
If a function is to use arguments, it must declare variables that accept the
values of the arguments. These variables are called the formal parameters of
thefunction.
The formal parameters behave like other local variables inside the function
and are created upon entryinto thefunction and destroyed upon exit.
While calling a function, there are two ways that arguments can be passed to a
function:
Call Type Description
Call by value
This method copies the actual value of an argument into the formal
parameter of the function. In this case, changes made to the
parameter inside the function have no effect on the argument.
Call by reference
This method copies the address of an argument into the formal
parameter. Inside the function, the address is used to access the
actual argument used in the call. This means that changes made to
the parameter affect the argument.
Fibonacci series in c using for loop
#include<stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of termsn");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-n",n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%dn",next);
}
return 0;
}
Fibonacci series program in c using recursion
#include<stdio.h>
#include<conio.h>
int Fibonacci(int);
main()
{
clrscr();
int n, i = 0, c;
printf("Enter any numbern");
scanf("%d",&n);
printf("Fibonacci seriesn");
for ( c = 1 ; c <= n ; c++ )
{
printf("%dn", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
Factorial program in c using for loop
#include <stdio.h>
#include<conio.h>
void main()
{
int c, n, fact = 1;
printf("Enter a number to calculate it's
factorialn");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %dn", n, fact);
getch();
}
Factorial program in c using function
#include <stdio.h>
#include<conio.h>
long factorial(int);
void main()
{
int number;
long fact = 1;
printf("Enter a number to calculate it's factorialn");
scanf("%d", &number);
printf("%d! = %ldn", number, factorial(number));
getch();
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return result;
}
Factorial program in c using recursion
#include<stdio.h>
#include<conio.h>
long factorial(int);
void main()
{
int n;
long f;
printf("Enter an integer to find factorialn");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.n");
else
{
f = factorial(n);
printf("%d! = %ldn", n, f);
}
getch();
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
Program to Find HCF and LCM
C program to find hcf and lcm using recursion
C program to find hcf and lcm using function
Program to sum the digits of number using recursive function
#include <stdio.h>
#include<conio.h>
int add_digits(int);
void main()
{
int n, result;
scanf("%d", &n);
result = add_digits(n);
printf("%dn", result);
getch();
}
int add_digits(int n) {
int sum = 0;
if (n == 0)
{
return 0;
}
sum = n%10 + add_digits(n/10);
return sum;
}
Function in c

More Related Content

PDF
Pointers and call by value, reference, address in C
Syed Mustafa
 
PPTX
sorting and searching.pptx
ParagAhir1
 
PPTX
User defined functions in C
Harendra Singh
 
PPTX
C function presentation
Touhidul Shawan
 
PPTX
User defined functions
Rokonuzzaman Rony
 
PPTX
Functions in C
Shobhit Upadhyay
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
Pointers and call by value, reference, address in C
Syed Mustafa
 
sorting and searching.pptx
ParagAhir1
 
User defined functions in C
Harendra Singh
 
C function presentation
Touhidul Shawan
 
User defined functions
Rokonuzzaman Rony
 
Functions in C
Shobhit Upadhyay
 
Function in C program
Nurul Zakiah Zamri Tan
 

What's hot (20)

PPTX
Preprocessor directives in c language
tanmaymodi4
 
PPTX
Exception Handling in object oriented programming using C++
Janki Shah
 
PDF
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
PPT
Pointers in c
Mohd Arif
 
PPT
Strings Functions in C Programming
DevoAjit Gupta
 
PPT
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
PPT
control-statements, control-statements, control statement
crrpavankumar
 
PDF
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
PPTX
Function in c program
umesh patil
 
PPT
User Defined Functions
Praveen M Jigajinni
 
PPTX
Function in c
Raj Tandukar
 
PPTX
Array Of Pointers
Sharad Dubey
 
PPTX
Union in c language
tanmaymodi4
 
ODP
Linguagem C 06 Funcoes
Regis Magalhães
 
PDF
C Pointers
omukhtar
 
PPTX
Operators and expressions in c language
tanmaymodi4
 
PPTX
Exception handling c++
Jayant Dalvi
 
PPTX
Unit 2. Elements of C
Ashim Lamichhane
 
DOCX
Practical File of C Language
RAJWANT KAUR
 
Preprocessor directives in c language
tanmaymodi4
 
Exception Handling in object oriented programming using C++
Janki Shah
 
STRUCTURE AND UNION IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Pointers in c
Mohd Arif
 
Strings Functions in C Programming
DevoAjit Gupta
 
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
control-statements, control-statements, control statement
crrpavankumar
 
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
Function in c program
umesh patil
 
User Defined Functions
Praveen M Jigajinni
 
Function in c
Raj Tandukar
 
Array Of Pointers
Sharad Dubey
 
Union in c language
tanmaymodi4
 
Linguagem C 06 Funcoes
Regis Magalhães
 
C Pointers
omukhtar
 
Operators and expressions in c language
tanmaymodi4
 
Exception handling c++
Jayant Dalvi
 
Unit 2. Elements of C
Ashim Lamichhane
 
Practical File of C Language
RAJWANT KAUR
 
Ad

Similar to Function in c (20)

PPTX
Detailed concept of function in c programming
anjanasharma77573
 
PPTX
C Programming Language Part 7
Rumman Ansari
 
PPTX
C function
thirumalaikumar3
 
PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
PPTX
Functionincprogram
Sampath Kumar
 
PPT
function_v1.ppt
ssuser823678
 
PPT
function_v1.ppt
ssuser2076d9
 
DOCX
Array Cont
Ashutosh Srivasatava
 
PPT
function_v1fgdfdf5645ythyth6ythythgbg.ppt
RoselinLourd
 
PPT
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
RoselinLourd
 
DOC
Unit 4 (1)
psaravanan1985
 
PPTX
Fundamentals of functions in C program.pptx
Dr. Chandrakant Divate
 
PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
PDF
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
PDF
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
PPT
Functions and pointers_unit_4
Saranya saran
 
PPTX
unit_2.pptx
Venkatesh Goud
 
PPT
c-Functions power point presentation on c functions
10300PEDDIKISHOR
 
PPTX
Presentation on function
Abu Zaman
 
PPTX
Dti2143 chapter 5
alish sha
 
Detailed concept of function in c programming
anjanasharma77573
 
C Programming Language Part 7
Rumman Ansari
 
C function
thirumalaikumar3
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
Functionincprogram
Sampath Kumar
 
function_v1.ppt
ssuser823678
 
function_v1.ppt
ssuser2076d9
 
function_v1fgdfdf5645ythyth6ythythgbg.ppt
RoselinLourd
 
functionsamplejfjfjfjfjfhjfjfhjfgjfg_v1.ppt
RoselinLourd
 
Unit 4 (1)
psaravanan1985
 
Fundamentals of functions in C program.pptx
Dr. Chandrakant Divate
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
Chapter 1. Functions in C++.pdf
TeshaleSiyum
 
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum
 
Functions and pointers_unit_4
Saranya saran
 
unit_2.pptx
Venkatesh Goud
 
c-Functions power point presentation on c functions
10300PEDDIKISHOR
 
Presentation on function
Abu Zaman
 
Dti2143 chapter 5
alish sha
 
Ad

More from CGC Technical campus,Mohali (20)

PPTX
Gender Issues CS.pptx
CGC Technical campus,Mohali
 
PPTX
Intellectual Property Rights.pptx
CGC Technical campus,Mohali
 
PPTX
Cyber Safety ppt.pptx
CGC Technical campus,Mohali
 
PPTX
Python Modules .pptx
CGC Technical campus,Mohali
 
PPT
Dynamic allocation
CGC Technical campus,Mohali
 
PPT
Control statments in c
CGC Technical campus,Mohali
 
PPTX
Operators in c by anupam
CGC Technical campus,Mohali
 
PPTX
Datatypes in c
CGC Technical campus,Mohali
 
PPT
Fundamentals of-computer
CGC Technical campus,Mohali
 
PPT
File handling-c
CGC Technical campus,Mohali
 
PPTX
Structure in C language
CGC Technical campus,Mohali
 
PPTX
Function in c program
CGC Technical campus,Mohali
 
PPTX
Data processing and Report writing in Research(Section E)
CGC Technical campus,Mohali
 
PPTX
data analysis and report wring in research (Section d)
CGC Technical campus,Mohali
 
PPTX
Section C(Analytical and descriptive surveys... )
CGC Technical campus,Mohali
 
Gender Issues CS.pptx
CGC Technical campus,Mohali
 
Intellectual Property Rights.pptx
CGC Technical campus,Mohali
 
Cyber Safety ppt.pptx
CGC Technical campus,Mohali
 
Python Modules .pptx
CGC Technical campus,Mohali
 
Dynamic allocation
CGC Technical campus,Mohali
 
Control statments in c
CGC Technical campus,Mohali
 
Operators in c by anupam
CGC Technical campus,Mohali
 
Fundamentals of-computer
CGC Technical campus,Mohali
 
Structure in C language
CGC Technical campus,Mohali
 
Function in c program
CGC Technical campus,Mohali
 
Data processing and Report writing in Research(Section E)
CGC Technical campus,Mohali
 
data analysis and report wring in research (Section d)
CGC Technical campus,Mohali
 
Section C(Analytical and descriptive surveys... )
CGC Technical campus,Mohali
 

Recently uploaded (20)

PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PPT
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
Zero Carbon Building Performance standard
BassemOsman1
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Information Retrieval and Extraction - Module 7
premSankar19
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 

Function in c

  • 2. Introduction A function is a block of code performing a specific task which canbe calledfrom other parts of a program. The name of the function is unique in a C Program and is Global. It means that a function can be accessed from any location with in aCProgram. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returnsnothing.
  • 3. Program to find if the number is Armstrong or not #include<stdio.h> #include<conio.h> int cube(int); void main() { clrscr(); int num, org, remainder,sum=0; printf("Enter any numbern"); scanf("%d",&num); org=num; while(num!=0) { remainder=num%10; sum=sum+cube(remainder); num=num/10; } if(org==sum) { printf("nThe number is armstrong"); } else { printf("nThe number is not armstrong"); } getch(); } int cube(int n) { int qbe; qbe=n*n*n; return qbe; }
  • 4. Second way: #include<stdio.h> #include<conio.h> int sum_cube_digits(int); void main() { clrscr(); int num, org, f_call; printf("Enter any numbern"); scanf("%d",&num); org=num; f_call=sum_cube_digits(num); if(org==f_call) { printf("nThe number is armstrong"); } else { printf("nThe number is not armstrong"); } getch(); } int sum_cube_digits(int n) { int rem ,sum=0; while(n!=0) { remainder=n%10; sum=sum+rem*rem*rem; n=n/10; } return sum; }
  • 5. Third way: #include<stdio.h> #include<conio.h> int armstrong(int); void main() { clrscr(); int num; printf("Enter any numbern"); scanf("%d",&num); armstrong(num); getch(); } int armstrong(int n) { int remainder,sum=0,org; org=n; while(n!=0) { remainder=n%10; sum=sum+remainder*remainder*remaind er; n=n/10; } if(org==sum) { printf("nThe number is armstrong"); } else { printf("nThe number is not armstrong"); } return(0); }
  • 6. Program to calculate factorial of a number. #include <stdio.h> #include <conio.h> int calc_factorial (int); // ANSI function prototype void main() { clrscr(); int number; printf("Enter a numbern"); scanf("%d", &number); calc_factorial (number);// argument ‘number’ is passed getch(); } int calc_factorial (int i) { int loop, factorial_number = 1; for (loop=1; loop <=i; loop++) factorial_number *= loop; printf("The factorial of %d is %dn",i, factorial_number); }
  • 7. Function Prototype int calc_factorial (int);  The prototype of a function provides the basic information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains.  The only difference between the header and the prototype is the semicolon;there must the a semicolonat the end of the prototype.
  • 8. Defining a Function:  Thegeneralform of a functiondefinition is as follows: return_type function_name( parameter list ) { body of the function }  Return Type: 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 keywordvoid.  Function Name: This is the actual name of the function. The function name and the parameter list together constitute the function signature.  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. Parameter names are not important in function declaration only their type is required  Function Body: The function body contains a collection of statements that define what the functiondoes.
  • 9. More about Function Function declaration is required when you define a function in one source file and you call that function in another file. In such case you should declare the functionat the top of thefile callingthe function. Calling aFunction: While creating a Cfunction, you give a definition of what the function has to do.Touse a function,you will haveto callthat functionto perform the defined task. When a program calls a function,program control is transferred to the called function.A called function performs defined task and when its return statementis executedor when its function-endingclosing brace is reached,it returns program control back to the mainprogram. Tocalla function,you simply need to pass the requiredparameters along with function name, and if function returns a value,then you can store returned value.
  • 10. For Example #include <stdio.h> #include<conio.h> int max(int num1,int num2); /* function declaration*/ int main () { int a =100; /* local variable definition */int b =200; int ret; ret =max(a,b); /* callinga function to getmaxvalue*/ printf( "Maxvalue is :%dn", ret ); return 0; } /* function returning the max between two numbers */ int max(int num1,intnum2) { int result; /*local variable declaration */ if (num1 >num2) result =num1; else result =num2; return result; }
  • 11. Local and global variables  Local: These variables only exist inside the specific function that creates them. They are unknown to other functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreatedeach time a function is executedorcalled.  Global: These variables can be accessed by any function comprising the program. They do not get recreated if the function is recalled. To declare a global variable, declare it outside of all the functions. There is no general rule for where outside the functions these should be declared, but declaring them on top of the code is normally recommended for reasons of scope. If a variable of the same name is declared both within a function and outside of it, the function will use the variable that was declared within it and ignore the globalone.
  • 12. Function Arguments: If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of thefunction. The formal parameters behave like other local variables inside the function and are created upon entryinto thefunction and destroyed upon exit. While calling a function, there are two ways that arguments can be passed to a function: Call Type Description Call by value This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. Call by reference This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.
  • 13. Fibonacci series in c using for loop #include<stdio.h> int main() { int n, first = 0, second = 1, next, c; printf("Enter the number of termsn"); scanf("%d",&n); printf("First %d terms of Fibonacci series are :-n",n); for ( c = 0 ; c < n ; c++ ) { if ( c <= 1 ) next = c; else { next = first + second; first = second; second = next; } printf("%dn",next); } return 0; }
  • 14. Fibonacci series program in c using recursion #include<stdio.h> #include<conio.h> int Fibonacci(int); main() { clrscr(); int n, i = 0, c; printf("Enter any numbern"); scanf("%d",&n); printf("Fibonacci seriesn"); for ( c = 1 ; c <= n ; c++ ) { printf("%dn", Fibonacci(i)); i++; } return 0; } int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) ); }
  • 15. Factorial program in c using for loop #include <stdio.h> #include<conio.h> void main() { int c, n, fact = 1; printf("Enter a number to calculate it's factorialn"); scanf("%d", &n); for (c = 1; c <= n; c++) fact = fact * c; printf("Factorial of %d = %dn", n, fact); getch(); }
  • 16. Factorial program in c using function #include <stdio.h> #include<conio.h> long factorial(int); void main() { int number; long fact = 1; printf("Enter a number to calculate it's factorialn"); scanf("%d", &number); printf("%d! = %ldn", number, factorial(number)); getch(); } long factorial(int n) { int c; long result = 1; for (c = 1; c <= n; c++) result = result * c; return result; }
  • 17. Factorial program in c using recursion #include<stdio.h> #include<conio.h> long factorial(int); void main() { int n; long f; printf("Enter an integer to find factorialn"); scanf("%d", &n); if (n < 0) printf("Negative integers are not allowed.n"); else { f = factorial(n); printf("%d! = %ldn", n, f); } getch(); } long factorial(int n) { if (n == 0) return 1; else return(n * factorial(n-1)); }
  • 18. Program to Find HCF and LCM
  • 19. C program to find hcf and lcm using recursion
  • 20. C program to find hcf and lcm using function
  • 21. Program to sum the digits of number using recursive function #include <stdio.h> #include<conio.h> int add_digits(int); void main() { int n, result; scanf("%d", &n); result = add_digits(n); printf("%dn", result); getch(); } int add_digits(int n) { int sum = 0; if (n == 0) { return 0; } sum = n%10 + add_digits(n/10); return sum; }