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

Functions - 1

The document discusses functions in C programming. It defines a function as a block of code that performs a specific task. There are two types of functions: standard library functions that are predefined and user-defined functions that are created by the programmer. Functions provide modularity, reusability, easier debugging and readability to a program. The document explains function declaration, definition, parameters, return types, calling functions and provides examples.

Uploaded by

sahuashishcs
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Functions - 1

The document discusses functions in C programming. It defines a function as a block of code that performs a specific task. There are two types of functions: standard library functions that are predefined and user-defined functions that are created by the programmer. Functions provide modularity, reusability, easier debugging and readability to a program. The document explains function declaration, definition, parameters, return types, calling functions and provides examples.

Uploaded by

sahuashishcs
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

C Function

Introduction
• A function is a block of code that performs a specific task.

Types of function
There are two types of function in C programming:
1. Standard library functions
2. User-defined functions
Standard library functions

• The standard library functions are built-in functions in C


programming.
• These functions are defined in header files. For example,
• The printf() is a standard library function to send formatted output to the
screen (display output on the screen). This function is defined in the stdio.h
header file.
• The sqrt() function calculates the square root of a number. The function is
defined in the math.h header file.
User-defined function
• C allows to define functions according to your need. These functions
are known as user-defined functions. For example:
• Suppose, you need to create a circle and color it depending upon the
radius and color. You can create two functions to solve this problem:
• createCircle() function
• color() function
Advantages of user-defined function
• It provides modularity to your program's structure.
• It makes your code reusable.
• In case of large programs with thousands of code lines, debugging and
editing becomes easier if functions are used.
• It makes the program more readable and easy to understand.
A function declaration tells the compiler about a function's name, return
type, and parameters. A function definition provides the actual body of
the function.
Function Declaration
• General syntax for function declaration is,
returntype functionName(type1 parameter1, type2 parameter2,...);
• Like any variable or an array, a function must also be declared before its used.
• Function declaration informs the compiler about the function name,
parameters, and its return type.
• The actual body of the function can be defined separately. It's also called
as Function Prototyping.
•  Function declaration consists of 4 parts.
• Return type
• Function name
• Parameter list
• Terminating semicolon
Function Declaration (Contd…)
• Return type
• Return type specifies the type of value (int, float, char, double) that function is expected to
return to the program which called the function.
• In case your function doesn't return any value, the return type would be void
• Function Name
• Function name is an identifier and it specifies the name of the function.
• Parameter list
• The parameter list declares the type and number of arguments that the function expects when
it is called. 

Example
int max(int num1, int num2);

Parameter names are not important in function declaration only their type is required, so the following is
also a valid declaration-

int multiply(int , int );


Function definition Syntax
• General syntax of function definition is,
returntype functionName (type1 parameter1, type2 parameter2,...)
{
// function body goes here
}

• The first line returntype functionName(type1 parameter1, type2 parameter2,...) is known


as function header and the statement(s) within curly braces is called function body.

Note: While defining a function, there is no semicolon(;) after the parenthesis in the function header,
unlike while declaring the function or calling the function.
Function body
The function body contains the declarations and the statements necessary for performing the
required task. The body is enclosed within curly braces { ... } and consists of three parts.
•local variable declaration(if required).
•function statements to perform the task inside the function.
•a return statement to return the result evaluated by the function(if return type is void, then no
return statement is required).

Example:
Calling a Function
• To use a function, we need to call that function to perform the defined task.
• When a program calls a function, the program control is transferred to the called function.
• A called function performs a defined task and when its return statement is executed or when its
function-ending closing brace is reached, it returns the program control back to the main program.
• To call a function, you simply need to pass the required parameters along with the function name,
and if the function returns a value, then you can store the returned value.
Example 1
#include <stdio.h>
int max(int num1, int num2); // function declaration
int main ()
{
int a = 100; int b = 200; int ret; //local variable definition
ret = max(a, b); // calling a function to get max value
printf( "Max value is : %d\n", ret );
return 0;
}
int max(int num1, int num2) // function returning the max between two numbers
{
int result; // local variable declaration
if (num1 > num2)
result = num1;
else result = num2;
return result;
}
Example 2
#include<stdio.h>
int multiply(int a, int b); // function declaration
int main()
{
int i, j, result;
printf("Please enter 2 numbers you want to multiply...");
scanf("%d%d", &i, &j);
result = multiply(i, j); // function call
printf("The result of multiplication is: %d", result);
return 0;
}
int multiply(int a, int b)
{
return (a*b); // function definition, this can be done in one line
}

You might also like