3 Functions
3 Functions
-main() -printf
-scanf
-sqrt()
1. User-defined functions
• These functions must be defined by the programmer or user.
• Programmer has to write the coding for such functions and test
them properly before using them.
• The syntax of the function is given by the user so there is no
need to include any header files.
• For example: main(), swap(), sum(), etc., are some of the user-
defined functions.
#include <math.h>
#include <stdio.h>
int main()
{
Output:
double number, squareRoot; Enter a positive number
printf("Enter a number: "); 25
Squareroot = 5
scanf("%lf", &number);
// computing the square root
squareRoot = sqrt(number);
printf("Square root of %.2lf = %.2lf", number, squareRoot);
return 0;
}
2. Predefined (or) library functions
• These functions are already defined in the system libraries.
• Programmer can reuse the existing code in the system libraries
which is helpful to write error-free code.
• User must be aware of the syntax of the function.
Example:
For instance, sqrt() function is available in math.h library and its usage is
y= sqrt (x), where x= number must be positive.
If x value is 25, i.e., y = sqrt (25) then ‘y’ = 5.
#include<stdio.h>
int sum(int, int);
void main() { Sum = 30
int a,b,result;
printf("\nGoing to calculate 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;
}
User-defined vs library functions
User-defined Library
A programmer creates a function according A function whose prototypes are already
to the requirement of a program defined in the C library
Example: multiply(), sum() divide(), etc. are Example: printf(), sqrt(), strcpy(), etc.
the user defined or user created function in a
program.
Advantages of Functions
• It is much easier to write a structured program where a
large program can be divided into a smaller, simpler
task.
• Allowing the code to be called many times.
• Easier to read and update.
• It is easier to debug a structured program where there
error is easy to find and fix.
C function declaration, function call and function
definition
There are 3 aspects in each C function. They are,
• Function declaration or prototype – this informs compiler about
the function name, function parameters and return value’s data
type.
• Function call – this calls the actual function.
• Function definition – this contains all the statements to be
executed.
C function aspects Syntax
function declaration return_type function_name
(arguments list)
Examples:
int funct(…) /*return a type int*/
float funct2(…) /*returns a type float*/
void funct3(…) /*returns nothing*/
Types of functions
printf("Sum = %d",c);
}
int sum() //function with no arguments and return data type
{
int x=10,y=20,z=5;
printf("x = %d ; y = %d ; z = %d \n",x,y,z);
int sum = x+y+z;
return(sum);
}
A function with an argument or arguments and a return no
value
• A function has arguments.
• A calling function can pass values to function called, but calling
function not receive any value.
• Data is transferred from calling function but no data is
transferred from the called function to the calling function.
• Generally Output is printed in the Called function.
• A function that does not return any value cannot be used in an
expression it can be used only as independent statement.
#include <stdio.h>
int main()
{
void sum(float,float); //function with arguments and no return value
float x=10.56,y=7.22;
sum(x,y);
}
void sum(float a,float b) //function with arguments and no return value
{
float z = a+b;
printf("x + y = %f",z);
}
Output:
x + y = 17.780001
A function with arguments and returning a values
Step 1:
Move the top (n-1) disks from frompeg (left) to auxpeg (center)
Step 2:
Move the largest disk from frompeg (left) to topeg (right)
Step 3:
Move the (n-1) disks from auxpeg (center) to topeg (right)
Example:
• Suppose no of disks are 4 and the starting rod is ‘A’, the auxiliary
rod is ‘B’ and the ending rod is ‘C’.
• In each step first, we move the topmost node of peg ‘A’ to
auxiliary peg ‘B’ and then finally move to ‘C’ which is the desired
peg.
• Initially: All disks are stacked on peg ‘A’ with the order of their size
being Disk3 < Disk2 < Disk1
1. Peg A Peg B Peg C
Disc 3
Disc 2
Disc 1
2. Moving Disc 3 To Peg C:
Peg A Peg B Peg C
Disc 2
Disc 1 Disc 3
3. Moving Disc 2 To Peg B:
Peg A Peg B Peg C
Disc 3
Disc 1 Disc 2
5. Moving Disc 1 To Peg C:
Peg A Peg B Peg C
Disc 3
Disc 2 Disc 1
6. Moving Disc 3 To Peg B:
Peg A Peg B Peg C
Disc 3 Disc 2
Disc 1
8. Moving Disc 3 To Peg C:
Peg A Peg B Peg C
Disc 3
Disc 2
Disc 1
Tower of Hanoi Algorithm: