Function in c

C
CGC Technical campus,MohaliCGC Technical campus,Mohali
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
1 de 22

Recomendados

C functions por
C functionsC functions
C functionsUniversity of Potsdam
9.9K vistas32 diapositivas
C function presentation por
C function presentationC function presentation
C function presentationTouhidul Shawan
438 vistas17 diapositivas
Function in c por
Function in cFunction in c
Function in csavitamhaske
1.3K vistas18 diapositivas
Function in c por
Function in cFunction in c
Function in cRaj Tandukar
6.8K vistas22 diapositivas
C Pointers por
C PointersC Pointers
C Pointersomukhtar
28K vistas14 diapositivas
Control Flow Statements por
Control Flow Statements Control Flow Statements
Control Flow Statements Tarun Sharma
8.4K vistas36 diapositivas

Más contenido relacionado

La actualidad más candente

Function in c program por
Function in c programFunction in c program
Function in c programumesh patil
1.4K vistas23 diapositivas
STRINGS IN C MRS.SOWMYA JYOTHI.pdf por
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
316 vistas24 diapositivas
String c por
String cString c
String cthirumalaikumar3
8.5K vistas16 diapositivas
FUNCTIONS IN c++ PPT por
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
25.4K vistas24 diapositivas
Functions in c language por
Functions in c language Functions in c language
Functions in c language tanmaymodi4
2.7K vistas30 diapositivas
Function in C program por
Function in C programFunction in C program
Function in C programNurul Zakiah Zamri Tan
70.5K vistas23 diapositivas

La actualidad más candente(20)

Function in c program por umesh patil
Function in c programFunction in c program
Function in c program
umesh patil1.4K vistas
STRINGS IN C MRS.SOWMYA JYOTHI.pdf por SowmyaJyothi3
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3316 vistas
FUNCTIONS IN c++ PPT por 03062679929
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
0306267992925.4K vistas
Functions in c language por tanmaymodi4
Functions in c language Functions in c language
Functions in c language
tanmaymodi42.7K vistas
RECURSION IN C por v_jk
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk14.1K vistas
Functions in c language por Tanmay Modi
Functions in c languageFunctions in c language
Functions in c language
Tanmay Modi606 vistas
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf por SowmyaJyothi3
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdfUSER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
USER DEFINED FUNCTIONS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3470 vistas
structure and union por student
structure and unionstructure and union
structure and union
student15.4K vistas
Operators and expressions in C++ por Neeru Mittal
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal4.8K vistas
Constructor and Types of Constructors por Dhrumil Panchal
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal2.8K vistas

Similar a Function in c

C Programming Language Part 7 por
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
466 vistas23 diapositivas
C function por
C functionC function
C functionthirumalaikumar3
495 vistas26 diapositivas
CH.4FUNCTIONS IN C_FYBSC(CS).pptx por
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxSangeetaBorde3
32 vistas31 diapositivas
Functionincprogram por
FunctionincprogramFunctionincprogram
FunctionincprogramSampath Kumar
44 vistas24 diapositivas
function_v1.ppt por
function_v1.pptfunction_v1.ppt
function_v1.pptssuser823678
12 vistas33 diapositivas
Array Cont por
Array ContArray Cont
Array ContAshutosh Srivasatava
456 vistas86 diapositivas

Similar a Function in c(20)

C Programming Language Part 7 por Rumman Ansari
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
Rumman Ansari466 vistas
CH.4FUNCTIONS IN C_FYBSC(CS).pptx por SangeetaBorde3
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde332 vistas
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx por vekariyakashyap
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap13 vistas
Chapter_1.__Functions_in_C++[1].pdf por TeshaleSiyum
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
TeshaleSiyum2 vistas
Chapter 1. Functions in C++.pdf por TeshaleSiyum
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
TeshaleSiyum9 vistas
Functions and pointers_unit_4 por Saranya saran
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran82 vistas
Presentation on function por Abu Zaman
Presentation on functionPresentation on function
Presentation on function
Abu Zaman11.2K vistas
Dti2143 chapter 5 por alish sha
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha736 vistas
UNIT3.pptx por NagasaiT
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
NagasaiT6 vistas

Más de CGC Technical campus,Mohali

Gender Issues CS.pptx por
Gender Issues CS.pptxGender Issues CS.pptx
Gender Issues CS.pptxCGC Technical campus,Mohali
13 vistas8 diapositivas
Intellectual Property Rights.pptx por
Intellectual Property Rights.pptxIntellectual Property Rights.pptx
Intellectual Property Rights.pptxCGC Technical campus,Mohali
15 vistas19 diapositivas
Cyber Safety ppt.pptx por
Cyber Safety ppt.pptxCyber Safety ppt.pptx
Cyber Safety ppt.pptxCGC Technical campus,Mohali
218 vistas24 diapositivas
Python Modules .pptx por
Python Modules .pptxPython Modules .pptx
Python Modules .pptxCGC Technical campus,Mohali
40 vistas11 diapositivas
Dynamic allocation por
Dynamic allocationDynamic allocation
Dynamic allocationCGC Technical campus,Mohali
188 vistas15 diapositivas
Arrays in c por
Arrays in cArrays in c
Arrays in cCGC Technical campus,Mohali
62 vistas25 diapositivas

Más de CGC Technical campus,Mohali(20)

Último

sam_software_eng_cv.pdf por
sam_software_eng_cv.pdfsam_software_eng_cv.pdf
sam_software_eng_cv.pdfsammyigbinovia
19 vistas5 diapositivas
Basic Design Flow for Field Programmable Gate Arrays por
Basic Design Flow for Field Programmable Gate ArraysBasic Design Flow for Field Programmable Gate Arrays
Basic Design Flow for Field Programmable Gate ArraysUsha Mehta
10 vistas21 diapositivas
GPS Survery Presentation/ Slides por
GPS Survery Presentation/ SlidesGPS Survery Presentation/ Slides
GPS Survery Presentation/ SlidesOmarFarukEmon1
7 vistas13 diapositivas
IRJET-Productivity Enhancement Using Method Study.pdf por
IRJET-Productivity Enhancement Using Method Study.pdfIRJET-Productivity Enhancement Using Method Study.pdf
IRJET-Productivity Enhancement Using Method Study.pdfSahilBavdhankar
10 vistas4 diapositivas
AWS Certified Solutions Architect Associate Exam Guide_published .pdf por
AWS Certified Solutions Architect Associate Exam Guide_published .pdfAWS Certified Solutions Architect Associate Exam Guide_published .pdf
AWS Certified Solutions Architect Associate Exam Guide_published .pdfKiran Kumar Malik
6 vistas121 diapositivas
Pitchbook Repowerlab.pdf por
Pitchbook Repowerlab.pdfPitchbook Repowerlab.pdf
Pitchbook Repowerlab.pdfVictoriaGaleano
9 vistas12 diapositivas

Último(20)

Basic Design Flow for Field Programmable Gate Arrays por Usha Mehta
Basic Design Flow for Field Programmable Gate ArraysBasic Design Flow for Field Programmable Gate Arrays
Basic Design Flow for Field Programmable Gate Arrays
Usha Mehta10 vistas
IRJET-Productivity Enhancement Using Method Study.pdf por SahilBavdhankar
IRJET-Productivity Enhancement Using Method Study.pdfIRJET-Productivity Enhancement Using Method Study.pdf
IRJET-Productivity Enhancement Using Method Study.pdf
SahilBavdhankar10 vistas
AWS Certified Solutions Architect Associate Exam Guide_published .pdf por Kiran Kumar Malik
AWS Certified Solutions Architect Associate Exam Guide_published .pdfAWS Certified Solutions Architect Associate Exam Guide_published .pdf
AWS Certified Solutions Architect Associate Exam Guide_published .pdf
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R... por IJCNCJournal
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...
Trust Metric-Based Anomaly Detection via Deep Deterministic Policy Gradient R...
IJCNCJournal5 vistas
MongoDB.pdf por ArthyR3
MongoDB.pdfMongoDB.pdf
MongoDB.pdf
ArthyR351 vistas
REACTJS.pdf por ArthyR3
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
ArthyR339 vistas
Ansari: Practical experiences with an LLM-based Islamic Assistant por M Waleed Kadous
Ansari: Practical experiences with an LLM-based Islamic AssistantAnsari: Practical experiences with an LLM-based Islamic Assistant
Ansari: Practical experiences with an LLM-based Islamic Assistant
M Waleed Kadous12 vistas
taylor-2005-classical-mechanics.pdf por ArturoArreola10
taylor-2005-classical-mechanics.pdftaylor-2005-classical-mechanics.pdf
taylor-2005-classical-mechanics.pdf
ArturoArreola1037 vistas
Web Dev Session 1.pptx por VedVekhande
Web Dev Session 1.pptxWeb Dev Session 1.pptx
Web Dev Session 1.pptx
VedVekhande23 vistas
GDSC Mikroskil Members Onboarding 2023.pdf por gdscmikroskil
GDSC Mikroskil Members Onboarding 2023.pdfGDSC Mikroskil Members Onboarding 2023.pdf
GDSC Mikroskil Members Onboarding 2023.pdf
gdscmikroskil72 vistas
Programmable Logic Devices : SPLD and CPLD por Usha Mehta
Programmable Logic Devices : SPLD and CPLDProgrammable Logic Devices : SPLD and CPLD
Programmable Logic Devices : SPLD and CPLD
Usha Mehta27 vistas

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; }