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

Functions in C

Uploaded by

Mrs.Minu Meera M
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Functions in C

Uploaded by

Mrs.Minu Meera M
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

JUSTICE BASHEER AHMED SAYEED COLLEGE FOR

WOMEN
(Autonomous) Afternoon Session Chennai 18.
S.I.E.T.

Prepared by
M. MINU MEERA, Assistant Professor
A. JUNAITHA BARVEEN, Assistant
Professor

06/22/24 Presenter Name: M.MINU MEERA DEPARTMENT OF COMPUTER


,A.JUNAITHA BARVEEN, Department SCIENCE 1
of Computer Science
• Keep the flow of control in a program as simple
as possible.
• Use top-down design.
– Keep decomposing (also known as factoring) a
problem into smaller problems until you have a
collection of small problems that you can easily solve.

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 2


of Computer Science
• C programs normally consist of a collection of
user-defined functions.
– Each function solves one of the small problems
obtained using top-down design.
– Functions call or invoke other functions as needed.

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 3


of Computer Science
scanf()
printf() A series of Instructions
getc() that are to be executed
more than once
putc()

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 4


of Computer Science
USER DEFINED FUNCTION : Some Terminology
SYNTAX : Header : Everything before the first brace.
return_datatype Body : Everything between the braces.
function_name(arguments) Type : Type of the value
{ returned by the
Body of the function function.
statements; Parameter List: A list of identifiers that
return; provide information for use within the
} body of the function. Also called formal
parameters.
call the function from main() :
syntax :
function_name(arguments );

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 5


of Computer Science
#include<stdio.h>
void hai() //definition
{
printf(" Welcome to functions\n");
printf("Good Morning\n");
}
void main()
{
clrscr();
printf("Main, Welcome to functions\n");
hai(); //calling
printf("Bye");
getch();
}
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 6
CSC COMPUTER EDUCATION,
M.K.B.NAGAR of Computer Science
(Based on Return values and passing Arguments)
 NO ARGUMENT NO RETURN VALUES

 ARGUMENT BUT NO RETURN VALUES


 NO ARGUMENT WITH RETURN VALUES
 WITH ARGUMENT WITH RETURN VALUES

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 7


of Computer Science
NO ARGUMENT NO RETURN VALUES
/* To perform Addition of two numbers */
/* NO ARGUMENT NO RETURN VALUES */
#include<stdio.h>
void add();
void main()
{
add();
printf(“main ends here");
add();
}
void add()
{
int a,b,c;
printf("Enter two numbers\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("The sum is %d\n",c);
}

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 8


of Computer Science
ARGUMENT BUT NO RETURN VALUES

/* To perform Addition of two numbers */


/* WITH ARGUMENT BUT NO RETURN VALUES*/
#include<stdio.h>
void add(int,int);
void main()
{
int x,y;
printf("Enter two number");
scanf("\t\t%d %d",&x,&y);
add(x,y); /* Actual Arguments */
}
void add(int a,int b) /* Formal Arguments */
{
int c=a+b;
printf("\t\tThe C Value is %d",c);
}

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 9


of Computer Science
The return Statement

• When a return statement is executed, program control is immediately


passed back to the calling environment.
– If an expression follows the keyword return, the value of the
expression is returned to the calling environment as well.
return;
return expression;

If There is No return statement


•Control is passed back to the calling environment when the closing brace
of the body is encountered.
•Known as “falling of the end.”

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 10


of Computer Science
NO ARGUMENT WITH RETURN VALUES

/* To perform Addition of two numbers


Without Argument and With Return values
*/
#include<stdio.h>
#include<conio.h>
int add(); //declaration
void main()
{
int c;
c=add(); /* Return Variable - c */
printf("The sum of two numbers is %d",c);
}
int add()
{
int a,b,c;
printf("Enter two Numbers=");
scanf("%d %d",&a,&b);
c=a+b;
return(c);
06/22/24
CSC COMPUTER EDUCATION,
} M.MINU MEERA ,A.JUNAITHA BARVEEN, Department
Presenter Name: 11
M.K.B.NAGAR of Computer Science
WITH ARGUMENT WITH RETURN VALUES

/* To perform Addition of two numbers


With Argument and With Return values */
#include<stdio.h>
int add(int,int); //Function prototype declaration
void main()
{
int c;
printf("Enter two Numbers=");
scanf("%d %d",&a,&b);
c=add(a,b); /* Actual Arguments */
printf("The sum of two numbers is %d",c);
}
int add(int x,int y) /* Formal arguments */
{
int c;
c=x+y;
return(c);
}

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 12


of Computer Science
Demo Program – Using a Function
to Calculate the Minimum of 2 Values
#include <stdio.h>
int min(int a, int b);
int main(void)
{
int j, k, m;
printf(“Input two integers: “);
scanf(“%d%d”, &j, &k);
m = min(j, k);
printf(“\nOf the two values %d and %d, “
“the minimum is %d.\n\n”, j, k, m);
return 0;
}
int min(int a, int b)
{
if (a < b)
return a;
else
return b;
}
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 13
of Computer Science
Function Prototypes

• A function prototype tells the compiler:


– The number and type of arguments that are to be passed to the function.
– The type of the value that is to be returned by the function.
• General Form of a Function Prototype
type function_name( parameter type list);
• Example
double sqrt(double);
• The parameter list is typically a comma-separated list of types.
• Identifiers are optional.
void f(char c, int i);
is equivalent to
void f(char, int);

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 14


of Computer Science
The Keyword void

• void is used if:

– A function takes no arguments.

– If no value is returned by the function.

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 15


of Computer Science
Function Invocation

• As we have seen, a function is invoked (or


called) by writing its name and an appropriate
list of arguments within parentheses.
– The arguments must match in number and type the
parameters in the parameter list of the function
definition.

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 16


of Computer Science
Call-by-Value

• In C, all arguments are passed call-by-value.


– This means that each argument is evaluated, and
its value is used in place of the corresponding
formal parameter in the called function.

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 17


of Computer Science
Demonstration Program
for Call-by-Value
#include <stdio.h>
int compute_sum(int n);
int main(void)
{
int n = 3, sum;
printf(“%d\n”, n); /* 3 is printed */
sum = compute_sum(n);
printf(“%d\n”, n); /* 3 is printed */
printf(“%d\n”, sum);
return 0;
}
int compute_sum(int n)
{
int sum = 0;
for (; n > 0; --n) /* in main(), n is unchanged */
sum += n;
printf(“%d\n”, n); /* 0 is printed */
return sum;
}
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 18
of Computer Science
Standard Style for Function Definition Order

#include <stdio.h>
#include <stdlib.h>
list of function prototypes
int main(void)
{
...
}
int max(int a, int b)
{
...
}
int min(int a, int b)
{
...
}
void prn_random_numbers(int k)
{
...
}

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 19


of Computer Science
Alternate Style for Function Definition Order

#include <stdio.h>
#include <stdlib.h>

int max(int a, int b)


{
...
}
int min(int a, int b)
{
...
}
void prn_random_numbers(int k)
{
...
}
int main(void)
{
...
}

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 20


of Computer Science
 The function is allowed access the actual
memory location(Address) of the argument
(original variable) and therefore can change
the value of the arguments of the calling
routine have to be changed.

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 21


of Computer Science
/*CALL BY REFERENCE as well as to swap 2 numbers
using pointers */
#include<stdio.h>
void swap(int *,int *);
void main()
{
int a,b;
printf("\nEnter the numbers to swap");
scanf("%d %d",&a,&b);
printf("The values before swapping :");
printf("\n%d %d",a,b);
swap(&a,&b);
}
Continue….
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 22
of Computer Science
void swap(int *e,int *f)
{
int *temp;
*temp=*e;
*e=*f;
*f=*temp;
printf("\n The swapped values are %d %d",*e,*f);
}

Enter the numbers to swap = 5 NOTE :


6
The values before swapping : 5 6 &  Address of
The swapped values are : 6 5
*  Content of

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 23


of Computer Science
Functions are easier to write and understand

 The arguments are seperated by commas


 The body of the function may consist of one or many
statements
 It cannot be defined within another function
 Function prototype is a function declaration that specifies
the data types of the arguments
 Calling one function from within another is said to be
nesting of Function calls
 main() returns an integer which is generally the operating
system
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 24
of Computer Science
Session Summary

 A function is a self contained program segment (block of statements) that performs

some specific well defined task.

Three steps in using a function are defining a function, prviding a prototype and calling

the function.

 Return statement is used to return the information from the function to the calling

portion of the program

 Scope of a variable is defined as the region over which the variable is visible or valid.

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 25


of Computer Science
EXERCISES

1. Write a program to sort the numbers in ascending order using functions?

2. Write a program to calculate xn using functions?

3. Write a program to check whether the year is leap year or not using functions?

4. Write a program to find the square of first N Numbers and to calculate its sum?

5. Write a program to swap two numbers using functions?

06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 26


of Computer Science
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 27
of Computer Science

You might also like