C- Strings and functions
C- Strings and functions
Strings in C
• A String in C programming is a sequence of
characters terminated with a null character
‘\0’.
• The C String is stored as an array of
characters.
• The difference between a character array
and a C string is that the string in C is
terminated with a unique character ‘\0’.
C String Declaration Syntax
• Declaring a string in C is as simple as
declaring a one-dimensional array.
• basic syntax for declaring a string:
char string_name[size];
example : char str[50]
• char str[] = {
'c','p','r','o','g','r','a','m','m','i','n','g','\0'};
// C program to illustrate // displaying the length of
strings string
#include <math.h>
#include <stdio.h>
int main()
{
int number, squareRoot;
printf("Enter a number: ");
scanf("%d", &number);
// computing the square root
squareRoot = sqrt(number);
printf("Square root of %d = %d", number, squareRoot);
return 0;
}
output: Enter a number: 25
Square root of 25 = 5
What are User Defined Functions?
• Definition: These functions which are written by the
programmer/user to do some specific tasks are called
user defined functions (UDFs).
Without Function With Function
#include <stdio.h>
#include <stdio.h>
void add()
int main()
{
{
int n1, n2, sum;
int n1, n2, sum;
printf("Enter two integers: ");
scanf("%d %d", &n1, &n2);
printf("Enter two integers: ");
// calculating sum
scanf("%d %d", &n1, &n2);
sum = n1 + n2;
printf("%d + %d = %d", n1, n2,
// calculating sum sum);
sum = n1 + n2; }
printf("%d + %d = %d", n1, 2, int main()
sum);
{
return 0;
add();
}
}
Elements of user-defined
functions
• The elements of user-defined functions
are shown below:
• Function definition: The program module
that is written to achieve a specific task is
Called function definition.
For example, consider the function shown below:
void add()
{
// calculating sum
sum = n1 + n2;
syntax:
return_type function_name(arguments);
ex : int add(int , int ) ;
#include <stdio.h>
void add()
{
}
Function definition
• Definition: The program module that is
written to achieve a specific task is called
function definition. The various elements
of the function definition are shown below:
Function call:
• In general, a function call is nothing but
invoking a function at the required place in
the program.
• that is after defining a function calling it or
invoking is called as function call.
int main()
{
/* This statement transfers the
add(); control to add() function */
}
#include <stdio.h>
/* Function declarations */
int max(int n1, int n2);
int main()
{
int num1, num2, maximum, minimum;
/* Input two numbers from user */
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);
maximum = max(num1, num2); // Call maximum function
printf("\nMaximum = %d\n", maximum);
return 0;
}
int max(int n1, int n2)
{
if(n1>n2)
return n1;
else
return n2;
}
Formal parameters
int main()
{
int num1, num2, maximum, minimum;
int max(int n1, int n2)
printf("Enter any two numbers: "); {
scanf("%d%d", &num1, &num2);
if(n1>n2)
maximum = max(num1, num2);
return n1;
printf("\nMaximum = %d\n", else
maximum);
return 0; return n2;
} }
Actual parameters
• The variables that are used while calling the function are
called actual parameters.
• The parameters passed to function are called actual
parameters.
• Actual parameters sends values to the formal
parameters
– above program that the function sum() do not receive any values
from the function main() and it does not return any value to the
function main().
• Functions with no parameters and return
values
– In this category, there is no data transfer
from the calling function to the called
function. But, there is data transfer from
called function to the calling function.
– Observe from the below program that the
function sum() do not receive any values from
the function main().
– But, it accepts data from the keyboard, find the
sum of those numbers and returns the result to
the calling function.
• Functions with parameters and no return
values
– In this category, there is data transfer from the
calling function to the called function using
parameters. But, there is no data transfer from
called function to the calling function.
• Observe from the above program that the function sum() receives two values
from the function main(), finds the sum of these numbers and display the
result there itself. The result is not passed to the calling function, but only
control is transferred.
• Functions with parameters and return
values
– In this category, there is data transfer between
the calling function and called function.
– When parameters are passed, the called
function can receive values from the calling
function. When the function returns a value,
the calling function can receive a value from
the called function.
• Observe from the above program that the function sum() receives two
values from the function main(), finds the sum of these numbers and sends
the result back to the calling function.
Passing Parameter To
Functions
1. Pass by value:
In this parameter passing method, values of actual parameters are
copied to function’s formal parameters and the two types of
parameters are stored in different memory locations.
So any changes made inside functions are not reflected in actual
parameters of the caller.
This method is also called as call by value.
#include<stdio.h> void swap(int x, int y)
void swap(int*, int*); {
int main() int t;
{ t = x;
int num1 = 10, num2 = 20; x = y;
swap(num1, num2); y = t;
printf("num1=%d num2=%d\n", num1, printf("x=%d y=%d\n", x, y);
num2);
}
return 0; }
2. Pass by reference :
Both the actual and formal parameters refer to the same
locations, so any changes made inside the function are
actually reflected in actual parameters of the caller.
This method is also called as call by reference.