Introduction
The slides cover the following topics:
Functions in C Programming ---------------------------------------Next slide
What is a function
Functions in C Programming ---------------------------------------Next slide
Advantages of function
Example
How the function works
Function prototypes
Syntax for Function Prototype
return-type function_name( arg-type name-1,...,arg-type name-n);
Function Prototype Examples
Function Definitions
Functions in C Programming ---------------------------------------Next slide
Function Definition Examples
float conversion (float celsius)
{
float fahrenheit;
fahrenheit = celcius33.8
return fahrenheit;
}
The function name’s is conversion
This function accepts arguments celcius of the type float. The function return a float value.
So, when this function is called in the program, it will perform its task which is to convert fahrenheit by multiply celcius with 33.8 and return the result of the summation.
Note that if the function is returning a value, it needs to use the keyword return.
Functions in C Programming ---------------------------------------Next slide
Function return types
Can be any of C’s data type:
char
int
float
long………
Examples:
int func1(...) / Returns a type int. /
float func2(...) / Returns a type float. /
void func3(...) / Returns nothing. /
Functions in C Programming ---------------------------------------Next slide
Types of Functions
Function can be divided into 4 categories:
Functions in C Programming ---------------------------------------Next slide
A function with no arguments and no return value
#include<stdio.h>
#include<conio.h>
void printline();
void main()
{
printf("Welcome to function in C");
printline();
printf("Function easy to learn.");
printline();
getch();
}
void printline()
{
int i;
printf(" ");
for(i=0;i<30;i++)
{ printf("-"); }
printf(" ");
}
Functions in C Programming ---------------------------------------Next slide
A function with no arguments and a return value
#include <stdio.h>
#include <conio.h>
int send();
void main()
{
int z;
z=send();
printf(" You entered : %d.",z);
getch();
}
int send()
{
int no1;
printf("Enter a no: ");
scanf("%d",&no1);
return(no1);
}
Functions in C Programming ---------------------------------------Next slide
A function with an argument or arguments and returning no value
#include<stdio.h>
#include<conio.h>
void add(int x, int y);
void main()
{
add(30,15);
add(63,49);
add(952,321);
getch();
}
void add(int x, int y)
{
int result;
result = x+y;
printf("Sum of %d and %d is %d. ",x,y,result);
}
Functions in C Programming ---------------------------------------Next slide
A function with arguments and returning a values
#include <stdio.h>
#include <conio.h>
int add(int x,int y);
void main()
{
int z;
z=add(952,321);
printf("Result %d. ",add(30,55));
printf("Result %d. ",z);
getch();
}
int add(int x,int y)
{
int result;
result = x + y;
return(result);
}
Send 2 integer value x and y to add()
Function add the two values and send back the result to the calling function
int is the return type of function
Return statement is a keyword and in bracket we can give values which we want to return.
Functions in C Programming ---------------------------------------Next slide
Pointers
Variable that declared occupies a memory according to it size
It has address for the location so it can be referred later by CPU for manipulation
The ‘’ and ‘&’ Operator
Int x= 10
We can use the address which also point the same value.
Functions in C Programming ---------------------------------------Next slide
#include <stdio.h>
#include <conio.h>
void main()
{
int i=9;
printf("Value of i : %d ",i);
printf("Adress of i %d ", &i);
getch();
}
Functions in C Programming ---------------------------------------Next slide
#include <stdio.h>
#include <conio.h>
void main()
{
int i=9;
printf("Value of i : %d ",i);
printf("Address of i %d ", &i);
printf("Value at address of i: %d", (&i));
getch();
}
Functions in C Programming ---------------------------------------Next slide
Passing arguments by value or reference
Functions in C Programming ---------------------------------------Next slide
Passing arguments by value or reference
#include <stdio.h>
#include <conio.h>
void callByValue(int, int);
void callByReference(int , int );
int main()
{
int x=10, y =20;
printf("Value of x = %d and y = %d. ",x,y);
printf(" CAll By Value function call... ");
callByValue(x,y);
printf(" Value of x = %d and y = %d. ", x,y);
printf(" CAll By Reference function call... ");
callByReference(&x,&y);
printf("Value of x = %d and y = %d. ", x,y);
getch();
return 0;
}
Functions in C Programming ---------------------------------------Next slide
void callByValue(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf(" Value of x = %d and y = %d inside callByValue function",x,y);
}
void callByReference(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
Functions in C Programming ---------------------------------------Next slide
1. What is a function in C programming? | ![]() |
2. How do you declare a function in C programming? | ![]() |
3. What is the purpose of a return statement in a function? | ![]() |
4. Can a function in C programming have multiple return statements? | ![]() |
5. How do you call a function in C programming? | ![]() |